简体   繁体   中英

jQuery's `on` doesn't see children elements

I want to have an event listener on my main div ( .main ). I want to detect clicks on article 's children header . My code attempts are as follow:

var area = $('article.small-post').children('.inner').children('header');

var area = $('article.small-post > .inner > header');

var area = 'article.small-post > .inner > header';

$('.main').on('click', area, function() {
     alert('bam');
});

None of the area code works. Only when I put single article.small-post , code fires. Where's my mistake?

HTML:

<div class="main">
<article class="small-post">
    <div class="inner">
        <header>
            title
        </header>
        <div class="entry">
            entry
        </div>
        <footer class="footer"></footer>
    </div>
</article>
</div>

First, only area3 will work because .on second parameter accept a string.

Then, you have an extra bracket :

$('.main').on('click', area#, function() {alert('bam');}});
                                                        ^-here

Remove it and it work

$('.main').on('click', area3, function() {alert('bam');});

http://jsfiddle.net/ondd6nw7/

Maybe this helps you. Of course you can put $main directly inline.

var $main = $(".main");

$main.on("click", "header", function() {
    alert("header clicked");
});

See my fiddle

You can try this:

$('.main article header').on('click', function() {
    // some code...
});

or try this:

$('.main article').on('click', 'header', function() {
    // some code...
});

You can find the difference explained here .

I hope this helps.

Why don't you do it like this?

$('.main article header').click(function(){
  alert('bam');
});

This was the alert will be triggered on all header elements that are in an article tag inside the main class.

You want to use Event Delegation to have all child clicks detected from one parent element.

An example would look like this:

$('.my-parent-class').on('click','.my-inner-element',doSomething);

More info here: http://api.jquery.com/on/

If I got you right, this might solve your problem.

See the JSfiddle link: http://jsfiddle.net/90xn59ot/3/

JS code:

// Little modifications
var area = $('article').children('div.inner').children('header'),
area2 = $('article > .inner > header'),
area3 = 'article > .inner > header';

// Basically with the single element
// If you have more than one element
// -> simply loop over them...
// 
// @example
// 
// for(var i = 0; i < area.length; i++) {
//    $('div.main').on('click', area[i].tagName, function () {
//         #code goes here...
//    } 
// }

$('div.main').on('click', area[0].tagName, function () {
   alert('Activate me only');
   area[0].style.backgroundColor = getRandomColor();
   area[0].style.color = getRandomColor();
});

// This one is just for fun...
function getRandomColor() {
   var letters = '0123456789ABCDEF'.split('');
   var color = '#';
   for (var i = 0; i < 6; i++ ) {
   color += letters[Math.floor(Math.random() * 16)];
}
   return color;
}

Try this:

$(function(){
    var $main = $('.main');

    $main.on('click', function(e){
        var $target = $(e.target),
            $header = $('header');

        if($target.is($header)){
            //do something
        }
    });
});

By clicking on .main , you're sending a click event argument to the function which catches the click target . If that target is before declared $main , do something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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