欢迎各位兄弟 发布技术文章
这里的技术是共享的
下面两个是自己亲自做的 有大用
$(document).ready(function() {
$('.menuinputul ul').not($(this).siblings('ul')).css('display','none'); $(this).siblings('ul').toggle(); });
可以使用jQuery 遍历中的 not() 方法来排除某些元素,例如根据元素的id,class等排除,示例代码
1 | $("div.content *").not(".keep"); // 表示content类的div下除keep类以外的所有元素;另外,注意*表示所有元素 |
下面给出实例演示:删除content类的div下除keep类以外的所有元素
创建Html元素
1 2 3 4 5 6 7 8 9 10 11 12 13 | <div class="box"> <span>点击按钮删除下面绿色框中所有不是keep类的元素,keep类的元素用红色区分。</span> <div class="content"> <input type="checkbox" name="item"><span>萝卜</span> <input type="checkbox" name="item"><span>青菜</span> <input type="checkbox" name="item" class="keep"><span class="keep">小葱</span> <input type="checkbox" name="item" class="keep"><span class="keep">豆腐</span> <input type="checkbox" name="item"><span>土豆</span> <input type="checkbox" name="item"><span>茄子</span> <input type="text" value="我也不是keep类的"> </div> <input type="button" value="删除"></div> |
设置css样式
1 2 3 4 5 6 | div.box{width:300px;height:200px;padding:10px 20px;border:4px dashed #ccc;}div.box>span{color:#999;font-style:italic;}.keep{color:red;}div.content{width:250px;height:100px;margin:10px 0;border:1px solid green;}input{margin:10px;}input[type='button']{width:200px;height:35px;margin:10px;border:2px solid #ebbcbe;} |
编写jquery代码
1 2 3 4 5 6 7 | $(function(){ $("input:button").click(function() { $("div.content *").not(".keep").each(function() { // "*"表示div.content下的所有元素 $(this).remove(); }); });}) |
观察显示效果
jquery选择器众多,使用选取某个DOM的方法有很多种,如何在选取的某个元素集上面做一些排除呢?下面通过几个实例说明:
1.选择所有的img元素,排除class=phpernote的元素的个数:
附实例:jQuery中排除指定元素,同时选择剩下的所有元素
场景:某页面用了js延时加载技术处理所有图片,以改善用户体验,但是有几个图片不想延时加载,要求把它们单独挑出来。
研究了一下jQuery的API文档,搞掂了,jQuery真的很方便,贴在这里备份:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!doctype html><html> <head> <title>菩提树下的杨过</title> <script type="text/javascript" src="http://img.24city.com/js/jquery-1.4.3.min.js"></script> <script type="text/javascript"> $().ready(function(){ $("div:not([delay='false'])").css("color","#f00"); }) </script> </head> <body> <div>div 1</div> <div delay="false">div 2</div> <div>div 3</div> </body></html> |