jquery中not的用法[.not(selector)]

描述: 从匹配的元素集合中移除指定的元素。
    如果提供的jQuery对象代表了一组DOM元素,.not()方法构建一个新的匹配元素的jQuery对象,用于存放筛选后的元素。所提供的选择器是对每个元素进行测试;如果元素不匹配的选择将包括在结果中。
    从jQuery 1.4开始,.not()方法可以接受一个函数作为参数,这和.filter()方式是一样。如果该函数返回 true,那么当前元素就不会包含在结果中。
     <ul>
        <li>list item 1</li>
        <li>list item 2</li>
        <li>list item 3</li>
        <li>list item 4</li>
        <li>list item 5</li>
    </ul>
    $('li').not(':even').css('background-color', 'red');
    此调用的结果是列表项2和4背景色变成红色,因为它们不匹配选择(记得:even 和 :odd使用基于0的索引),:even为偶数项,not(:even)即奇数项,但索引从0开始,里面item2索引为1

猜你喜欢

转载自www.cnblogs.com/hahajava/p/9044188.html