如何隐藏具有特定属性集的所有元素?
我通过来自 php 文档的 ajax 请求将一系列项目插入到我的 html 文档中。返回的数据由一堆具有自定义属性集的 div 元素组成,以便我能够判断哪些 div 元素属于已返回的项目的哪个类别。我希望能够单击类别名称并隐藏具有自定义属性(设置为类别名称)的所有元素。
示例返回
<div class="row">
<div class="category" categoryID="category_1">
category_1
<div categoryID="category_1">item 1</div>
<div categoryID="category_1">item 2</div>
<div categoryID="category_1">item 3</div>
</div>
<div class="category" categoryID="category_2">
category_2
<div categoryID="category_2">item 1</div>
<div categoryID="category_2">item 2</div>
<div categoryID="category_2">item 3</div>
</div>
<div class="category" categoryID="category_3">
category_3
<div categoryID="category_3">item 1</div>
<div categoryID="category_3">item 2</div>
<div categoryID="category_3">item 3</div>
</div>
</div>
<div class="row">
<div class="category" categoryID="category_1">
category_1
<div categoryID="category_1">item 1</div>
<div categoryID="category_1">item 2</div>
<div categoryID="category_1">item 3</div>
</div>
<div class="category" categoryID="category_2">
category_2
<div categoryID="category_2">item 1</div>
<div categoryID="category_2">item 2</div>
<div categoryID="category_2">item 3</div>
</div>
<div class="category" categoryID="category_3">
category_3
<div categoryID="category_3">item 1</div>
<div categoryID="category_3">item 2</div>
<div categoryID="category_3">item 3</div>
</div>
</div>
我使用 jquery 来处理大部分 javascript 函数,因此在处理动态添加到 dom 的数据时,我自然会使用如下代码。
$(".skillCategory").live({
mouseenter:
function(){
$(this).css('background-color', 'white');
},
mouseleave:
function(){
$(this).css('background-color', '#393939');
}
});
$(".skillCategory").live('click',function(){
var title = $(".skillCategory").attr("categoryID");
});
});
我试图能够隐藏一个类别以及由“categoryID”属性确定的属于该类别的所有项目。
感谢您的时间和帮助。
I have a series of items being inserted into my html document via ajax request from a php document. The data that is returned consists of a bunch of div elements with custom attributes set so that I may be able to tell which div elements belong to which category of items that have been returned. I would like to be able to click on the category name and hide all elements that have the custom attribute (which is set to the category name).
Sample return
<div class="row">
<div class="category" categoryID="category_1">
category_1
<div categoryID="category_1">item 1</div>
<div categoryID="category_1">item 2</div>
<div categoryID="category_1">item 3</div>
</div>
<div class="category" categoryID="category_2">
category_2
<div categoryID="category_2">item 1</div>
<div categoryID="category_2">item 2</div>
<div categoryID="category_2">item 3</div>
</div>
<div class="category" categoryID="category_3">
category_3
<div categoryID="category_3">item 1</div>
<div categoryID="category_3">item 2</div>
<div categoryID="category_3">item 3</div>
</div>
</div>
<div class="row">
<div class="category" categoryID="category_1">
category_1
<div categoryID="category_1">item 1</div>
<div categoryID="category_1">item 2</div>
<div categoryID="category_1">item 3</div>
</div>
<div class="category" categoryID="category_2">
category_2
<div categoryID="category_2">item 1</div>
<div categoryID="category_2">item 2</div>
<div categoryID="category_2">item 3</div>
</div>
<div class="category" categoryID="category_3">
category_3
<div categoryID="category_3">item 1</div>
<div categoryID="category_3">item 2</div>
<div categoryID="category_3">item 3</div>
</div>
</div>
I am using jquery to handle most of the javascript function so naturally I would am using code like below when working with the data that is dynamically added to the dom.
$(".skillCategory").live({
mouseenter:
function(){
$(this).css('background-color', 'white');
},
mouseleave:
function(){
$(this).css('background-color', '#393939');
}
});
$(".skillCategory").live('click',function(){
var title = $(".skillCategory").attr("categoryID");
});
});
I am trying to be able to hide a category and all of the items in which belong to it as determined by the "categoryID" attribute.
Thank you for your time and assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我正确理解你的问题,这样的事情应该有效:
If i understand your question correctly, something like this should work:
您可以格式化 HTML 以使用新的“数据属性”表示法:
然后您可以执行以下操作:
You could format your HTML to use the new "data attribute" notation:
Then you can do this:
尝试使用jquery属性选择器: http://api.jquery.com/attribute-equals-selector /
您应该将其与其他内容结合起来以避免隐藏主 div,例如“:not(.skillCategory)”或类似内容。
Try using the jquery attribute selector : http://api.jquery.com/attribute-equals-selector/
You should combine it with something else to avoid hiding the main div, like ":not(.skillCategory)" or similar.
这将为您做到这一点:
http://jsfiddle.net/J6PRM/
希望它有所帮助。
This sould do it for you :
http://jsfiddle.net/J6PRM/
hope it helps.