我尝试用简单的方式解释这个问题。
我有一个自定义插件,它将点击事件添加到提醒文本字段值的文本字段。
function()
{
$(this).click(function()
{
alert($(this).val());
});
}
我将该插件称为 $(document).customPlugin()
。
现在如果我这样做:
var clonedTextField = $("#text-one").clone(true);
$("#text-one").replaceWith(clonedTextField);
单击克隆的文本字段后,我看不到它的值,而是看到我从(母文本字段)克隆的文本字段的值。
我需要当前文本字段(克隆文本字段)的值而不是母文本字段;
在这里查看问题: http://jsfiddle.net/Z2hdm/5/
最佳答案
原因是 plugs()
方法未附加到克隆元素。
.clone( [withDataAndEvents ] )
A Boolean indicating whether event handlers should be copied along with the elements. As of jQuery 1.4, element data will be copied as well.
据我所知,克隆元素将不包含第 3 方插件。因此,您需要将其重新附加到新的克隆元素,例如
$('input').replaceWith(cloned).plugs();
JSFiddle
关于javascript - 自定义插件在clone() JQuery之后引用错误的$(this),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21649799/