[jQuery] is

isについて。

by tea4two

HTML

<ul>
    <li>
        <em>em</em>list item 1</li>
    <li>
        <em>em-1</em>list item 2
        <em>em-2</em>
    </li>
    <li>list item 3</li>
    <li>list item 4</li>
    <li>list item 5</li>
</ul>

JavaScript

$("li").click(function() {
    var $li = $(this),
        twoEM = $li.is(function() {
            //this func. returns true / false
            return $('em', this).length === 2;
        });
    
    console.log(twoEM);
    
    if (twoEM) {
        $li.css("background-color", "green");
    } else {
        $li.css("background-color", "red");
    }
});