HTML 链接和 Jquery Live 只能在第一次尝试时单击一次才有效

发布于 2024-08-27 02:19:56 字数 1211 浏览 5 评论 0原文

我遇到了链接上 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月亮坠入山谷 2024-09-03 02:19:56

编辑:好的,我已经缩小了范围
问题。如果我取出 return false,
该事件每次都会触发。这
问题是页面跳转到
顶部。有什么想法可以阻止这种情况吗?

尝试

event.preventDefault();

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?

try

event.preventDefault();
你げ笑在眉眼 2024-09-03 02:19:56

既然跳到了顶部...实际的href内容是什么?如果它是类似“#”的东西,那么它很可能会跳到顶部。如果单击的结果是更改页面上的内容,而不是实际导航离开页面,您可以考虑使用:

<a href="javascript:void(0)">linky</a>

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:

<a href="javascript:void(0)">linky</a>
荒人说梦 2024-09-03 02:19:56

这可以防止链接跳转到顶部:

<div id="website-messages">
<ul>
<li><a href="javascript:;" >my link</a></li>
</ul>
</div>

这使得 js 每次都会触发!

$("div#website-messages ul li a").live("click", function(e) {
    //this make it fire every time!
    e.preventDefault();
    var link = $(this);
    changeTab(link.attr("href"));
    $(link.attr("title")).focus();
});

更新:

您的链接应该是这样的:

<li><a id='" + errors[i].elementID + "' href='javascript:;' title='" + errors[i].tabid + "'>" + errors[i].message + "</a></li>

this prevent link jumps to the top:

<div id="website-messages">
<ul>
<li><a href="javascript:;" >my link</a></li>
</ul>
</div>

this make js it fire every time!

$("div#website-messages ul li a").live("click", function(e) {
    //this make it fire every time!
    e.preventDefault();
    var link = $(this);
    changeTab(link.attr("href"));
    $(link.attr("title")).focus();
});

UPDATE:

your link should be something like this:

<li><a id='" + errors[i].elementID + "' href='javascript:;' title='" + errors[i].tabid + "'>" + errors[i].message + "</a></li>
枫林﹌晚霞¤ 2024-09-03 02:19:56

我在文本输入上发生了一个模糊事件,该事件专注于验证它们。如果我单击其中一个错误并聚焦到第一个文本框,然后单击第二个错误,它将聚焦到第二个文本框,但会触发模糊事件而不是聚焦。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文