jQuery - .trigger() 在 .load() 之后不起作用
这是我的代码:
function refresh () {
sub_tab=$("#hid").val();
$("#content").load(cur_tab+'.php',function () {
alert(sub_tab);
$("#pd").trigger('click');
});
}
行 $("#pd").trigger('click');
似乎不起作用。但是,如果我将这一行作为独立操作执行(没有 load
),它就可以工作。#pd
是一个
,它正在加载到包含上述脚本的页面中。有什么想法吗?
#pd 位于加载到 #content 的另一个 PHP 页面内部 。现在,当我单击 #pd 时,它工作正常,只是,当我将 PHP 文件重新加载到 #content 时,触发事件似乎不起作用
This is my code:
function refresh () {
sub_tab=$("#hid").val();
$("#content").load(cur_tab+'.php',function () {
alert(sub_tab);
$("#pd").trigger('click');
});
}
The line $("#pd").trigger('click');
doesn't seem to work. However, if I execute this line as a standalone operation (without load
), it works.#pd
is a <div>
which is being loaded into the page which contains the above script.
Any ideas?
#pd is inside another PHP page which is loaded into #content
. Now when i click #pd, it works fine, just that , the trigger event does not seem to work, when i reload the PHP file into #content
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
click
事件绑定到#pd
,并且#pd
位于#content
内部,则您将丢失每次重新加载内容时都会绑定,因为#pd
正在被销毁并重新创建。尝试使用 delegate 来绑定事件 - 它与 Ajax 调用和动态 DOM 配合得更好:
稍后,当您调用
$("#pd").trigger('click');
时,您应该会看到预期的结果。If the
click
event is bound to#pd
, and#pd
is inside of#content
, you lose the binding every time you reload the content, because#pd
is being destroyed and recreated.Try using delegate to bind the event - it plays much more nicely with Ajax calls and dynamic DOM:
Later, when you will call
$("#pd").trigger('click');
, you should see the expected results.