可能是一个简单的答案,但我不明白jQuery的原理足以解决它.
我有这个:
$(".remove", document.getElementById(itemsAreaId)).live("click", function(){
Run Code Online (Sandbox Code Playgroud)
我想迁移到哪个 .on()
我试过这样做
var selectedEls = $(".remove", document.getElementById(itemsAreaId));
$(document).on('click',selectedEls, function(e){
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
有谁解释我做错了什么?
如果可能,为什么我的解决方案不起作用,所以我可以理解.
非常感谢,
保罗
可选的选择器on() 必须是字符串.你正在为它提供一个jQuery对象(selectedEls).
相反,只需直接使用字符串选择器.remove.而不是目标document,使用itemsAreaId;
$('#' + itemsAreaId).on('click', '.remove', function ());
Run Code Online (Sandbox Code Playgroud)
如果你想要定位document,你可以使用后代选择器itemsAreaId和.remove,像这样;
$(document).on('click', '#' + itemsAreaId + ' .remove', function ());
Run Code Online (Sandbox Code Playgroud)
...但最好尽可能靠近源处理附加处理程序,因此首先将首选.