Linkify custom link in function

和自甴很熟 提交于 2019-12-04 05:29:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!