Jquery 单击函数不适用于动态元素
Jquery click function is not working for dynamic elements
我正在使用$.each
创建每个数组对象的按钮。我还尝试为每个按钮提供一个特定的 id,所以我可以做点击事件进行进一步编码,但现在我不知道为什么所有按钮都不起作用。我错过了一些代码吗?
var questlist = [{
"startdate": "2015-01-08",
"questitem": [
{
"questid": "1",
"gifttype": "stars",
"quantity": 10,
"questname": "One",
"queststatus": "done"
}, {
"questid": "2",
"gifttype": "stars",
"quantity": 50,
"questname": "Two",
"queststatus": "ready"
}, {
"questid": "3",
"gifttype": "stars",
"quantity": 100,
"questname": "Three",
"queststatus": "complete"
}, {
"questid": "4",
"gifttype": "stars",
"quantity": 120,
"questname": "Four",
"queststatus": "done"
}, {
"questid": "5",
"gifttype": "stars",
"quantity": 220,
"questname": "Five",
"queststatus": "ready"
},
]
}];
questitemlist(questlist);
function questitemlist() {
var callquest = "<div class='questlist_container'>" +
"<div id='call_questitem'></div>" +
"</div>";
$("#call_quest").append(callquest);
var questlistobj = questlist[0].questitem;
$.each(questlistobj, function(i, obj) {
if (obj.queststatus == "ready") {
var questlist_item_button = "<input type='button' id='questlist_item_button_go" + obj.questid + "' class='questlist_item_button' id='questlist_item_button' value='GO !'/>";
$("#questlist_item_button_go" + obj.questid).click(function() {
alert("go");
});
console.log("#questlist_item_button_go" + obj.questid);
} else if (obj.queststatus == "done") {
var questlist_item_button = "<input type='button' id='questlist_item_button_reward" + obj.questid + "' class='questlist_item_button' id='questlist_item_button' value='REWARD !'/>";
$("#questlist_item_button_reward" + obj.questid).click(function() {
alert("reward");
});
} else if (obj.queststatus == "complete") {
var questlist_item_button = "<label class='questlist_item_complete'><img class='questlist_item_img' src='img/check.png'/></label>";
}
var questlist_item = "<div class='questlist_item'>" +
questlist_item_button +
"<label class='questlist_item_questname'>" + obj.questname + "</label>" +
"<label class='questlist_item_gifttype'>" + obj.gifttype + " " + obj.quantity + " " + "</label>" +
"</div>";
$("#call_questitem").append(questlist_item);
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="call_quest"></div>
click()
函数,您可以调用具有直接绑定的元素。直接绑定将仅附加在 DOM 加载时存在的事件处理程序,即静态元素。
如果在加载 DOM 后创建了元素,则在未正确附加事件处理程序的情况下,不会触发与这些元素关联的所有事件。
当你创建动态元素时,这意味着它们是在加载 DOM 后创建的,并且在直接绑定时它们不存在,所以你不能直接调用click()
。
如果要获取动态创建的元素click
功能,可以使用 on 创建委托绑定。这可以通过将.on
处理程序添加到静态父元素来实现。
Delegated events have the advantage that they can process events from descendant elements that
are added to the document at a later time.
更改此行
$("#questlist_item_button_reward" + obj.questid).click(function() {
alert("reward");
});
自
$("#call_questitem").on("click", "#questlist_item_button_reward" + obj.questid, function() {
alert("reward");
});
并对go
按钮执行相同的操作。
演示
您正在用 questlist_item_button
覆盖动态 id。
<input
type='button'
id='questlist_item_button_go"+obj.questid+"'
class='questlist_item_button'
id='questlist_item_button' <!-- REMOVE ME -->
value='GO !'/>
那是因为你的 DOM 将是动态创建的。所以你必须在jQuery中使用delegate:
绑定具有选定 ID 的文档上的单击事件:
$(document).on('click', '#questlist_item_button_go'+obj.questid, function(){
// your action here
});
您必须使用.on()
函数为动态创建的元素附加事件。
例
$(document).on('click', '#DYNAMIC_ELEMENT_ID', function(){
// your action here
});
相关文章:
- 点击功能没有'ajax调用动态元素(Backbone)后无法工作
- 将动态元素绑定到函数;只剩下一个绑定
- 关于动态元素中循环中的事件
- 对动态元素使用.on()和e.stopPropagation()
- 只删除javascript中的1个动态元素
- 删除在加载dom之后创建的动态元素
- 从服务器和/或客户端加载动态元素的典型技术是什么
- IE7/8中的jQuery动态元素错误
- Jquery动态元素选择器
- AnguarJS$编译的动态元素
- 使用jQuery使动态元素可选择
- jquery在动态元素上调用日期选择器
- 如果在transclusion之后添加,为什么指令下的动态元素具有错误的作用域
- 如何使用jQuery唯一地识别具有相同id的类似动态元素
- 将单击处理程序绑定到动态元素
- 为动态元素附加事件处理程序
- 使用 jQuery 创建动态元素 单击事件始终返回 for 循环中的最后一个索引
- jQuery中的目标动态元素
- 将动态元素/行添加到 Angularjs 中的多维 Json 数组中
- Jquery 单击函数不适用于动态元素