nested element firing parent event

Isaac Gonzalez

I have a jsp that generates the following html structure:

<ul class="folders" id="yui_patched_v3_11_0_1_1410259364795_727">
    <li class="folder" style="cursor: pointer;" id="yui_patched_v3_11_0_1_1410259364795_726">
        <i class="fa fa-plus"></i> <i class="fa fa-folder"></i> Prueba 1 
        <ul class="files" style="display: block;">
            <li class="file"> <a href="/documents/10184/10675/welcome_tools" target="_blank" class="link"> <i class="fa fa-file"></i> welcome_tools </a> </li>
            <li class="file"> <a href="/documents/10184/10675/welcome_learn" target="_blank" class="link"> <i class="fa fa-file"></i> welcome_learn </a> </li>
            <li class="file"> <a href="/documents/10184/10675/welcome_cube" target="_blank" class="link"> <i class="fa fa-file"></i> welcome_cube </a> </li>
            <li class="file"> <a href="/documents/10184/10675/welcome_community" target="_blank" class="link"> <i class="fa fa-file"></i> welcome_community </a> </li>
        </ul>
        <ul class="folders" id="yui_patched_v3_11_0_1_1410259364795_725" style="display: block;">
            <li class="folder" style="cursor: pointer;" id="yui_patched_v3_11_0_1_1410259364795_724">
                <i class="fa fa-folder-open-o"></i> Sub 1 
                <ul class="files" style="display: block;">
                    <li class="file"> <a href="/documents/10184/10707/helpful_links_for_using_liferay_portal" target="_blank" class="link"> <i class="fa fa-file"></i> helpful_links_for_using_liferay_portal </a> </li>
                </ul>
                <ul class="folders" style="display: block;" id="yui_patched_v3_11_0_1_1410259364795_798">
                    <li class="folder" style="cursor: pointer;" id="yui_patched_v3_11_0_1_1410259364795_797">
                        <i class="fa fa-folder-open-o"></i> Sub Sub 1 
                        <ul class="folders" style="display: block;"> </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

And i have the following javascript

$("li.folder")
    .css("cursor", "pointer")
    .on("click", function(){

        $this = $(this);
        $this.find("ul.files,ul.folders").toggle(200);

        return false;
});

For a tree-like functionality.

The problem is whenever i click an a it fires the li.folder event. How can i make it so that the link behaves as a link.

Codepen link: http://codepen.io/anon/pen/Hokgt

adeneo

You can just exclude the anchor

$("li.folder")
    .css("cursor", "pointer")
    .on("click", function(e){

        if ( ! $(e.target).is('a') ) {

            $(this).find("ul.files,ul.folders").toggle(200);

        }

        return false;
});

or go the other way, and stop the propagation

$("li.folder a").on('click', function(e) {
    e.stopPropagation();
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Appending an element to the child fires the drop event in the parent

分類Dev

JQuery - Keep the same event [mouseenter] in a nested element

分類Dev

GiveFeedback event not firing

分類Dev

jQuery onClick Event not firing

分類Dev

Mouseenter event for firing

分類Dev

Event is not firing in jQuery

分類Dev

jQuery 'click' event not firing

分類Dev

Nested Nav goes out of its parent anchor element

分類Dev

htKeyUp event not firing for certain keys

分類Dev

Firing a click event on a child component

分類Dev

JS Scroll Event not firing up

分類Dev

Mouseup event not firing with code inside function

分類Dev

ng-change event not firing inside the loop

分類Dev

mouseup event not firing when "dragging" an image in Chrome

分類Dev

auth0 lock authenticated event not firing

分類Dev

Javascript click event not firing the first time

分類Dev

Interactive Grid selection changed event is not firing

分類Dev

OnClick event firing but not visibly present in the .aspx

分類Dev

asp.net button event not firing

分類Dev

Scrolling the Datagrid firing mouse double click event

分類Dev

jQuery .append Onchange event firing multiple times

分類Dev

angularjs button event not firing when clicked

分類Dev

Ionic/AngularJS ng-click event not firing?

分類Dev

Vue.js "emit event" don't bubble up to parent and grandparent element

分類Dev

Vue.js "emit event" don't bubble up to parent and grandparent element

分類Dev

Select parent element in stylus

分類Dev

Bind event to wrapper element

分類Dev

Nested element with events

分類Dev

Function listed for the onClick event is firing at load time on ReactJS

Related 関連記事

ホットタグ

アーカイブ