jQuery可以操作插入的元素吗?
我是jQuery的新手。 我认为jQuery可以操作代码添加的元素是合理的,但我发现它现在不能。
$(function(){ $("#addVideo").click(function(){ $("#publisher").append(" "); }) $("#choseType input:eq(0)").click(function(){ $(this).addClass("selected"); }) })
这是jquery的限制还是我的代码的错? 看来如果我将第二个click()
函数放入第一个它将正确运行,但它只运行一次(如果我删除附加的#choseType
并再次追加它,第二次点击将不起作用。)。然后如何操纵代码添加元素? 谢谢!
$(function(){ $("#addVideo").click(function(){ //create new content, and append to publisher //also, reference the new content var newContent = $(" ").appendTo('#publisher'); //then add a handler to input in the context of the new content $("input:eq(0)", newContent).click(function(){ $(this).addClass("selected"); }); }) })
将事件委托给非动态元素:
$(document).on('click', "#choseType input:eq(0)", function(){ $(this).addClass("selected"); });
而ID是独一无二的!