扫码关注官方订阅号
1
2
3
欢迎选择我的课程,让我们一起见证您的进步~~
$(".show").click(function(){ var _this=$(this); $(".show").each(function(){ if($(this).text()!=_this.text()){ $(this).hide() } }) })
$(".show").click(function(event){ var target = event.target; $(target).siblings().hide(); $(target).show(); });
没必要再写循环,用siblings这个特性对兄弟节点操作也许会更好
1楼回答正确
换个思路,先把内容一样的元素放到一个对象数组里,然后点击的时候获取当前内容,最后遍历对象数组然后将对应的元素隐藏。
优点:当元素特别多的时候,不需要遍历所有的节点;缺点:页面加载就要遍历所有的元素节点。
let map = {}, $show = $('.show'), self = this; $show.each(function(index, item) { let $this = $(this); let content = $this.text(); if (!map[content]) { map[content] = []; } map[content].push($this) }); $show.click(function() { let $this = $(this), content = $this.text(); map[content].forEach(function(item, index, array) { item.hide(); },self); });
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
没必要再写循环,用siblings这个特性对兄弟节点操作也许会更好
1楼回答正确
换个思路,先把内容一样的元素放到一个对象数组里,然后点击的时候获取当前内容,最后遍历对象数组然后将对应的元素隐藏。
优点:当元素特别多的时候,不需要遍历所有的节点;
缺点:页面加载就要遍历所有的元素节点。