jQuery,输入中的前置数据无效

jQuery,输入中的前置数据无效,jquery,Jquery,我有以下HTML标记: <div id="destination"> </div> <input id="test" type="text" /> <button id="clicker">Click me</button>​ 警报文本实际上是字段中输入的文本,但文本不会在div#destination中结束 请参见此JSFIDLE示例: 您必须执行以下操作: $(document).on('click', '#clicker'

我有以下HTML标记:

<div id="destination">

</div>

<input id="test" type="text" />

<button id="clicker">Click me</button>​
警报文本实际上是字段中输入的文本,但文本不会在div
#destination
中结束

请参见此JSFIDLE示例: 您必须执行以下操作:

$(document).on('click', '#clicker', function(event){

    var newCat = $('#test').val();
    $("#destination").prepend(newCat );
       alert(newCat);
        });

为什么要尝试向值添加元素

var newCat = $('#test').val();
上一行将为您提供文本框的值

$(newCat) 

不会与任何元素对应。

我认为您正在交换源和目标,即:

$(document).on('click', '#clicker', function(event){
    var newCat = $('#test').val();
    $('#destination').prepend(newCat);
       alert(newCat);
});​

您使用的
prepend()
不正确。语法是:

$(targetElement).prepend(newContent);
在您的具体示例中使用如下方式:

$("#destination").prepend(newCat);

您为什么要将活动委托给
文档
?谢谢!在我的原始文件(在我制作JSFIDLE之前)中,我使用了prependTo。无论如何,你的答案是有效的:)
$("#destination").prepend(newCat);