0

The following code causes my browsers to crash. For Firefox, i've been given an option whether or not i want to stop jQuery because it was running longer than it should be.

div id="divLeaving">
    You are about to leave to: <span id="spanLeavingURL"></span>
    <a id="divLeavingYes" href="#">Yes</a><a id="divLeavingNo" href="#">No</a>
</div>
<a href="http://www.google.com">google</a>

<script type="text/javascript" language="javascript">

    $(document).ready(function() {
        $("a:not(#divLeavingYes, #divLeavingNo)").click(function() {
            var url = $(this).attr("href");
            var test_if_local = url.indexOf("mycompany.com");
            if (test_if_local == -1) {
                $("#spanLeavingURL").text(url);
                $("#divLeavingYes").attr("href", url);
                $("#divLeavingNo").click(function() { $(this).dialog('destroy'); });
                $("#divLeaving").dialog({ modal: true });
                return false;
            }
        });
    });
</script>

However, if i remove the not selector it doesn't crash my browser.

<div id="divLeaving">
    You are about to leave to: <span id="spanLeavingURL"></span>
    <a id="divLeavingYes" href="#">Yes</a><a id="divLeavingNo" href="#">No</a>
</div>
<a href="http://www.google.com">google</a>

<script type="text/javascript" language="javascript">

    $(document).ready(function() {
        $("a").click(function() {
            var url = $(this).attr("href");
            var test_if_local = url.indexOf("mycompany.com");
            if (test_if_local == -1) {
                $("#spanLeavingURL").text(url);
                $("#divLeavingYes").attr("href", url);
                $("#divLeavingNo").click(function() { $(this).dialog('destroy'); });
                $("#divLeaving").dialog({ modal: true });
                return false;
            }
        });
    });
</script>

How do i change thes query so it selects all except for #divLeavingYes and #divLeavingNo?

4

3 回答 3

1

我不知道 jQuery 的实现,但是:

a:not(#divLeavingYes, #divLeavingNo)

不是有效的 CSS3 选择器。:not 选择器只能采用简单的选择器。尝试:

a:not(#divLeavingYes):not(#divLeavingNo)
于 2009-09-11T22:06:30.113 回答
0

try with this, I include "event.stopPropagation();":

$(document).ready(function() {
    $("a:not(#divLeavingYes, #divLeavingNo)").click(function(event) {
    event.stopPropagation();
        var url = $(this).attr("href");
        var test_if_local = url.indexOf("mycompany.com");
        if (test_if_local == -1) {
            $("#spanLeavingURL").text(url);
            $("#divLeavingYes").attr("href", url);
            $("#divLeavingNo").click(function() { $(this).dialog('destroy'); });
            $("#divLeaving").dialog({ modal: true });
            return false;
        }
    });
});
于 2009-09-11T17:52:20.897 回答
0

我不确定你的 HTML 是如何构造的,但也许你可以让 jQuery 的 slice() 过滤器来解决这个问题......

http://docs.jquery.com/Traversing/slice

于 2009-09-11T19:13:55.567 回答