如何隐藏具有特定属性集的所有元素?

发布于 2024-11-26 11:07:32 字数 2431 浏览 0 评论 0原文

我通过来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

执笔绘流年 2024-12-03 11:07:32

如果我正确理解你的问题,这样的事情应该有效:

$(".category").live('click',function(){
       var category = $(this).attr("categoryID");
       $("[categoryID=" + category + "]").not('.category').hide();
    });

If i understand your question correctly, something like this should work:

$(".category").live('click',function(){
       var category = $(this).attr("categoryID");
       $("[categoryID=" + category + "]").not('.category').hide();
    });
踏雪无痕 2024-12-03 11:07:32

您可以格式化 HTML 以使用新的“数据属性”表示法:

<div class="category" data-categoryID="category_3">

然后您可以执行以下操作:

$(".category").click(function() {
  var ctx = $(this);
  $(".category").each(function(item) {
    if($(this).data("categoryID") != ctx.data("categoryID") {
      $(this).hide();
    }
  });
});

You could format your HTML to use the new "data attribute" notation:

<div class="category" data-categoryID="category_3">

Then you can do this:

$(".category").click(function() {
  var ctx = $(this);
  $(".category").each(function(item) {
    if($(this).data("categoryID") != ctx.data("categoryID") {
      $(this).hide();
    }
  });
});
酒废 2024-12-03 11:07:32

尝试使用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.

决绝 2024-12-03 11:07:32

这将为您做到这一点:

http://jsfiddle.net/J6PRM/

希望它有所帮助。

This sould do it for you :

http://jsfiddle.net/J6PRM/

hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文