如果选择器是从外部加载的,如何触发单击事件
How to trigger click event if selector is loaded from external
有人有类似的经历吗?我想通过使用.load("…")函数加载一个外部文件到当前页面。加载后,如何触发点击
中的图像?
脚本
$("#tab-3-list img.coupon_list").on("click", function (e) {}
没有功能。如果我将list.html的内容嵌入到当前正文中,上面的脚本就可以工作了。
<html lang="en" class="ui-mobile">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
</head>
<section id="home" data-role="page">
<article data-role="content">
<div id="home-content"></div>
</article>
<!-- content -->
</section>
<!-- home -->
<script type="text/javascript">
$(document).ready(function() {
$("#home-content").load("list.html");
$("#tab-3-list img.coupon_list").on("click", function (e) {
e.preventDefault();
window.alert(this.id);
});
});
</script>
</body>
</html>
外部文件list.html
<div id="tab-3-list">
<div class="ui-grid-a">
<div class="ui-block-a">
<img id="c01" class="coupon_list" src="/images/coupon/c01.png">
</div>
</div>
<!-- /ui-grid-a -->
</div>
<!-- /tab-3-list -->
尝试在此上下文中使用event-delegation
,因为您是在运行时加载DOM元素,
$("#home-content").on("click","#tab-3-list img.coupon_list",function (e) {
e.preventDefault();
alert(this.id);
});
try this.
<script type="text/javascript">
$(document).ready(function() {
$("#home-content").load("list.html", function(){
$("#tab-3-list img.coupon_list").on("click", function (e) {
e.preventDefault();
window.alert(this.id);
});
});
});
</script>
load
使用ajax,因此需要一些时间从外部页面加载数据。当您将click
event
附加到img.coupon_list
时,它甚至不存在于DOM(在页面中)。
试试这个,
$("#home-content").load("list.html", function () {
$("#tab-3-list img.coupon_list").on("click", function (e) {
e.preventDefault();
window.alert(this.id);
});
});
仅在返回load
的数据并将其添加到DOM时才附加click
。
使用事件委托- .on(),并使用.trigger()触发点击事件
$(document).ready(function() {
$("#home-content").load("list.html",
function(){
$('#tab-3-list img.coupon_list').trigger('click');
});
$("#home-content").on("click","#tab-3-list img.coupon_list", function (e) {
e.preventDefault();
window.alert(this.id);
});
});
-just put event in live mode , it's work after reload :
<script type="text/javascript">
$(document).ready(function() {
$("#home-content").load("list.html");
$("#home-content").one("load",function(){
// everytime an image finishes loading
}).each(function () {
if (this.complete) {
// manually trigger load event in
// event of a cache pull
$(this).trigger("click");
}
});
$("#home-content #tab-3-list img.coupon_list").live("click", function (e) {
e.preventDefault();
window.alert(this.id);
});
}); </script>
相关文章:
- 在IE8中不起作用的元素上触发单击事件
- 如果确认消息,则触发单击事件
- 在带有 标记的树节点上以编程方式触发单击事件
- 如果释放鼠标时内部元素未悬停,则防止触发“单击”的正确方法
- 如何在禁用的元素上触发单击事件
- 如果元素是在循环中创建的,则引导下拉菜单不会触发单击事件
- 防止d3笔刷触发单击事件
- 图像映射区域无法触发单击事件.afer最大化寡妇或恢复窗口单击事件触发
- 在另一个函数中触发单击函数
- 空格键触发单击事件复选框
- 如何仅针对单击的图层触发单击事件,而不为其父层触发单击事件
- 如何在文本区域中触发单击事件
- 触发单击后,下一个委托不起作用
- 在冒泡的父项上触发单击事件
- 触发单击事件不起作用 jquery
- 从引导模式对话框中触发单击事件
- D3 - 防止父
元素触发单击事件 - 追加了 JQuery 的代码不会在输入文件上触发单击事件
- 余烬集成:在根元素上触发单击事件
- 触发单击并链接单击行为