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

如何在链接上使用jQuery,并使其在单击时在文本字段中显示链接?,jquery,Jquery,我想要达到的目标很简单。我只是不知道用jQuery的行话来实现它。我希望能够点击一个链接,然后链接输出到一个文本字段中。我想是这样的: $(document).ready(function(){ $('a.text-display').click(function(){ "copy that link text and display it in a textbox" }); }); 差不多了: $(document).ready(function(){ $('a.

我想要达到的目标很简单。我只是不知道用jQuery的行话来实现它。我希望能够点击一个链接,然后链接输出到一个文本字段中。我想是这样的:

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

$(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)
    });
});

你想要这样的吗

$(document).ready(function(){

    $('a.text-display').click(function(e){
        e.preventDefault(); //don't follow the link
        $(this).replaceWith('<input value="'+ $(this).attr("href") +'">');
    });
});​
$(文档).ready(函数(){
$('a.text-display')。单击(函数(e){
e、 preventDefault();//不跟随链接
$(此)。替换为(“”);
});
});​

在这里看到它的作用:

真棒!关于测试这个婴儿。preventDefault()类似于javascript:void(0)吗?不,它阻止了浏览器设置的默认事件(如单击链接时跟踪链接)好的,上面的脚本完成了最初的工作-但是我如何保持它,使链接不会在每次单击链接时清除字段?例如:如果我创建了A、B、C、D、E链接,我如何单击其中的每一个链接并使其显示在文本框“A、B、C、D、E”中?请查看我的更新答案。可以将第二个参数为当前值的函数传递到val()中。不,但那是个很酷的把戏。我需要所有的链接都在同一地点,它将只是在一个文本形式显示