0

我在下面有以下代码和 jsFiddle 链接。在我看来,我的代码应该可以工作,它会找到正确的 li 标签并禁用默认操作,问题是它会在不应该的情况下将其重复到子元素中。你能解释一下吗?

http://jsfiddle.net/andyjh07/6HAvV/

HTML:

<ul id="nav">
    <li class=""><a href="home.htm">Home</a>
    </li>
    <li class="nav_parent"><a href="our-projects.htm">Our Projects</a>
        <ul class="nav_child">
            <li class=""><a href="the-palms-at-corozal.htm">The Palms At Corozal</a>
            </li>
            <li class=""><a href="the-lakes-at-consejo.htm">The Lakes At Consejo</a>
            </li>
        </ul>
    </li>
    <li class="nav_parent"><a href="about-us.htm">About Us</a>
        <ul class="nav_child">
            <li class=""><a href="who-we-are.htm">Who We Are</a>
            </li>
            <li class=""><a href="other-projects.htm">Other Projects</a>
            </li>
            <li class=""><a href="partners.htm">Partners</a>
            </li>
        </ul>
    </li>
    <li class="nav_parent"><a href="about-belize.htm">About Belize</a>
        <ul class="nav_child">
            <li class=""><a href="general-info.htm">General Info</a>
            </li>
            <li class=""><a href="investing-in-belize.htm">Investing In Belize</a>
            </li>
            <li class=""><a href="useful-links.htm">Useful Links</a>
            </li>
        </ul>
    </li>
    <li class=""><a href="news.htm">News Articles</a>
    </li>
    <li class=""><a href="contact-us.htm">Contact Us</a>
    </li>
</ul>

jQuery:

$(document).ready(function () {
    $('ul#nav li:eq(1)').addClass('removeLink');
    $('.removeLink').click(function (e) {
        e.preventDefault();
    });
});
4

1 回答 1

1

问题是您绑定到li元素,并且由于子元素在 中li,它们也会触发click事件。

尝试更新并防止仅单击a元素:

$('.removeLink > a').click();

这是一个更新的 jsFiddle

于 2013-03-25T10:21:26.823 回答