问题
I am trying to create custom links based on different buttons using this html.
<input type="text" name="coachid" id="textbox1" value="22984">
<button class="txtLinky" id="Mickey">Mickey</button><br/>
<div id="linkified"></div>
<button class="txtLinky" id="Donald">Donald</button><br/>
<div id="linkified"></div>
Here is the javascript
$(".txtLinky").click(function () {
$('#linkified').html('http:www.example.com/' + $(this).attr('id') + '?referringpId=' + $("#textbox1").val());
$('#linkified').linkify({
tagName: 'a',
target: '_blank',
newLine: '\n'
});
$('#linkified').css('padding', '100px');
});
Not sure how to get the second button to work. The first button will work but not the second.
here is http://jsfiddle.net/VFhK9/4/
回答1:
You have multiple div
elements with id="linkified"
.
id
is unique; you cannot have more than one control with the same id.
You could change your id
attributes to class
attributes and do something like this:
HTML
<input type="text" name="coachid" id="textbox1" value="22984" />
<br />
<br/>
<button class="txtLinky" id="Mickey">Click Link</button>
<br/>
<div class="linkified">value</div>
<br/>
<button class="txtLinky" id="Donald">Click Link</button>
<br/>
<div class="linkified">value</div>
JQUERY
$(".txtLinky").on("click", function ()
{
var $this = $(this),
id = $this.attr("id"),
text = $("#textbox1").val();
$this
.nextAll('.linkified:first')
.css({ padding: '10px' })
.html
(
'http://www.example.com/'
+ id
+ '?referringRepId='
+ text
)
.linkify
({
tagName: 'a',
target: '_blank',
newLine: '\n'
});
});
See working jsFiddle demo
来源:https://stackoverflow.com/questions/24919462/linkify-custom-link-in-function