HTML 链接和 Jquery Live 只能在第一次尝试时单击一次才有效
我遇到了链接上 jquery 实时事件绑定的问题。当链接添加到页面时,它工作正常,但是当另一个链接添加到无序列表时,需要单击两次才能在任一链接上触发单击事件。有什么想法吗?
$("div#website-messages ul li a").live("click", function() {
var link = $(this);
changeTab(link.attr("href"));
$(link.attr("title")).focus();
return false;
});
编辑:好的,我已经缩小了问题的范围。如果我取出 return false,则该事件每次都会触发。问题是页面跳转到顶部。有什么想法可以阻止这种情况吗?
创建链接的代码:
Validation.validate = function() {
var html = "";
for (var i = 0; i < errors.length; i++) {
html += "<li><a href='" + errors[i].tabid + "' title='" + errors[i].elementID + "'>" + errors[i].message + "</a></li>";
}
$("div#website-messages ul").html(html);
}
ChangeTab Function
function changeTab(changeTo) {
changeTo = changeTo.substr(changeTo.indexOf("#"), changeTo.length);
$("#tabs div").hide();
$(changeTo).show();
$("ul.navigation li a").removeClass("selected");
$("ul.navigation li a[href='" + changeTo + "']").addClass("selected");
}
SOLVED 我在文本输入上发生了一个模糊事件,该事件专注于验证它们。如果我单击其中一个错误并聚焦到第一个文本框,然后单击第二个错误,它将聚焦到第二个文本框,但会触发模糊事件而不是聚焦。非常感谢大家的帮助和建议,这让我一整天都发疯。
I'm running into a problem with jquery live event binding on a link. When the link is added to the page it works fine, however when another link is added to the unordered list it requires two clicks for the click event to fire on either link. Any ideas?
$("div#website-messages ul li a").live("click", function() {
var link = $(this);
changeTab(link.attr("href"));
$(link.attr("title")).focus();
return false;
});
EDIT: OK I've narrowed down the problem. If I take out return false, the event fires every time. The problem is then the page jumps to the top. Any ideas to stop that?
Code that creates the links:
Validation.validate = function() {
var html = "";
for (var i = 0; i < errors.length; i++) {
html += "<li><a href='" + errors[i].tabid + "' title='" + errors[i].elementID + "'>" + errors[i].message + "</a></li>";
}
$("div#website-messages ul").html(html);
}
ChangeTab Function
function changeTab(changeTo) {
changeTo = changeTo.substr(changeTo.indexOf("#"), changeTo.length);
$("#tabs div").hide();
$(changeTo).show();
$("ul.navigation li a").removeClass("selected");
$("ul.navigation li a[href='" + changeTo + "']").addClass("selected");
}
SOLVED
I had a blur event on the text inputs that were being focused to that validated them. If I clicked on one of the errors and focused to the first textbox, then clicked on the second error, it would focus to the second textbox but fire the blur event and not focus. Thank you all SO much for your help and suggestions, this was driving me crazy all day.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
try
既然跳到了顶部...实际的href内容是什么?如果它是类似“#”的东西,那么它很可能会跳到顶部。如果单击的结果是更改页面上的内容,而不是实际导航离开页面,您可以考虑使用:
Since it jumps to the top ... what is the content of the actual href? If it is something like "#", then it will likely jump to the top. If the result of the click is to change the content on the page, instead of actually navigating away from the page, you might consider using:
这可以防止链接跳转到顶部:
这使得 js 每次都会触发!
更新:
您的链接应该是这样的:
this prevent link jumps to the top:
this make js it fire every time!
UPDATE:
your link should be something like this:
我在文本输入上发生了一个模糊事件,该事件专注于验证它们。如果我单击其中一个错误并聚焦到第一个文本框,然后单击第二个错误,它将聚焦到第二个文本框,但会触发模糊事件而不是聚焦。
I had a blur event on the text inputs that were being focused to that validated them. If I clicked on one of the errors and focused to the first textbox, then clicked on the second error, it would focus to the second textbox but fire the blur event and not focus.