如何过滤所选对象,然后隐藏未选中的对象
问题描述:
从昨天开始跟进...如何过滤所选对象,然后隐藏未选中的对象
这部分代码确实有效。
$(document).ready(function(){
$('#listMenu a').click(function (selected) {
var getPage = $(this).attr('id');
var getName = $(this).attr('name');
//console.log(getPage);
//console.log(getName);
$(function() {
$("#" + getName).show();
var getColor = $('#' + getName).css('background-color');
$('#header').css('background', getColor);
$('#slideshow').css('background', getColor);
$('span').css('background', getColor);
$('#menuContainer').css('background', getColor);
});
$('li.listMenu ul').hide();
});
});
我能够得到我想要的东西;基于vars getPage和 getName,现在我需要隐藏未选中的内容。
我已经试过这样:
$(function() {
var notSelected = $('div').filter(selected).attr('id', 'name');
$(this).hide();
console.log(notSelected);
});
放在略高于:$('li.listMenu ul').hide();
但它是不正确的,记得我是一个真正的新手。
我在屏幕上看到的是应该隐藏的新选定项目。
再次赞赏任何帮助。
-sjs
答
我相信你想这个,如果你点击一个<a>
一个DIV中:
$('div').not($(this).closest('div')).hide();
或者,如果你打算淡出其他<a>
元素:
$('#listMenu a').not(this).hide();
.not() function过滤那些匹配出来的元素,所以.hide()
将运行在其余的o f元素。在你的情况下,selected
是点击事件,你想要的是this
,它指的是点击来自的#listMenu a
。