如何在链接上使用jQuery并使其在单击时显示文本字段中的链接?

时间:2012-08-05 07:09:28

标签: jquery

我想要实现的目标很简单。我只是不知道jQuery的术语来实现它。我希望能够点击一个链接,链接输出到文本字段中的“链接”。我想它会是这样的:

$(document).ready(function(){
  $('a.text-display').click(function(){
    "copy that link text and display it in a textbox"
   });
});

2 个答案:

答案 0 :(得分:2)

几乎就在那里:

$(document).ready(function(){

    $('a.text-display').click(function(e){
        e.preventDefault(); //don't follow the link
        var linkText = $(this).text();
        $('#mytextfield').val(function(index, oldValue) {return oldValue + ' ' + linkText } ); //add the text to the current value of the textfield (input text)
    });
});

答案 1 :(得分:0)

你想要这样的东西吗?

$(document).ready(function(){

    $('a.text-display').click(function(e){
        e.preventDefault(); //don't follow the link
        $(this).replaceWith('<input value="'+ $(this).attr("href") +'">');
    });
});​

在此处查看此行动:http://jsfiddle.net/EJcRB/