Javascript jQuery:附加事件、调用事件
在代码的一部分中,我有以下内容:Javascript jQuery:附加事件、调用事件,javascript,jquery,triggers,bind,Javascript,Jquery,Triggers,Bind,在代码的一部分中,我有以下内容: $("#searchbar").trigger("onOptionsApplied"); $("#searchbar").bind("onOptionsApplied", function () { alert("fdafds"); }); 在代码的另一部分中,我有以下内容: $("#searchbar").trigger("onOptionsApplied"); $("#searchbar").bind("onOptionsApplied", f
$("#searchbar").trigger("onOptionsApplied");
$("#searchbar").bind("onOptionsApplied", function () {
alert("fdafds");
});
在代码的另一部分中,我有以下内容:
$("#searchbar").trigger("onOptionsApplied");
$("#searchbar").bind("onOptionsApplied", function () {
alert("fdafds");
});
bind()
在trigger()之前执行,但是当我查看页面时,我从未收到警报()
为什么不呢?我在事件中做错了什么?我也尝试过,效果很好
将bind函数放在ready处理程序中,然后触发事件…它应该会顺利运行我也尝试过,效果很好
将绑定函数放入就绪处理程序中,然后触发事件…它应该会平稳运行执行以下操作时,您的#searchbar
可能不是DOM的一部分:
// This will not work if #searchbar is not part of the DOM
// or if the DOM is not ready yet.
$("#searchbar").bind("onOptionsApplied", function () {
alert("fdafds");
});
如果在文档准备就绪之外,或者因为动态添加了#searchbar
,则通常会发生这种情况
要确保即使在这些条件下,应用的选项也正确绑定,请使用或:
// This can be outside document ready.
// It binds all now and future #searchbars
$("#searchbar").live("onOptionsApplied", function () {
alert("fdafds");
});
// Document ready
$(function() {
// Whatever you do....
// Make sure #searchbar is now part of the DOM
// Trigger onOptionsApplied
$("#searchbar").trigger("onOptionsApplied");
});
});
执行以下操作时,您的#搜索栏
可能不是DOM的一部分:
// This will not work if #searchbar is not part of the DOM
// or if the DOM is not ready yet.
$("#searchbar").bind("onOptionsApplied", function () {
alert("fdafds");
});
如果在文档准备就绪之外,或者因为动态添加了#searchbar
,则通常会发生这种情况
要确保即使在这些条件下,应用的选项也正确绑定,请使用或:
// This can be outside document ready.
// It binds all now and future #searchbars
$("#searchbar").live("onOptionsApplied", function () {
alert("fdafds");
});
// Document ready
$(function() {
// Whatever you do....
// Make sure #searchbar is now part of the DOM
// Trigger onOptionsApplied
$("#searchbar").trigger("onOptionsApplied");
});
});
您确定已将这些代码行放入就绪处理程序中吗?因为它对我很有效。您确定已将这些代码行放入就绪处理程序中吗?因为它对我很有效。