简体   繁体   English

HTML链接和Jquery Live仅在一次尝试时可以使用

[英]HTML Link and Jquery Live Only Works on First Try with one click

I'm running into a problem with jquery live event binding on a link. 我在链接上遇到jquery实时事件绑定问题。 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. 如果我取出return false,则每次触发该事件。 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 ChangeTab功能

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. 非常感谢大家的帮助和建议,这使我整日疯狂。

EDIT: OK I've narrowed down the problem. 编辑:确定,我已经缩小了问题的范围。 If I take out return false, the event fires every time. 如果我取出return false,则每次触发该事件。 The problem is then the page jumps to the top. 问题是页面跳到顶部。 Any ideas to stop that? 有什么想法可以阻止吗?


try 尝试

event.preventDefault();

Since it jumps to the top ... what is the content of the actual href? 由于它跳到顶部...实际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>

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! 这使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();
});

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>

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. 如果我单击一个错误并聚焦于第一个文本框,然后单击第二个错误,它将聚焦至第二个文本框,但触发模糊事件而不是聚焦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM