简体   繁体   English

如果没有HTML属性,则隐藏div

[英]Hide div if does not have HTML attribute

I have the following code, its supposed to reorder my div s within #list . 我有以下代码,它应该在#list重新排序div And it does that just fine. 它确实很好。 Now what I want to add is: if a div does not have the attribute "data-l-disc" don't add it/hide it. 现在,我要添加的是:如果div没有属性“ data-l-disc”,请不要添加/隐藏它。

<select id="selectid">
  <option id="valid1" value="def">Default order</option>
  <option id="valid2" value="hl">h-l</option>
</select>
<div id="list">
---------the follow div is used about 50*-------------
 <div class="dbl" data-l-disc="1">
 ---------blabla (like 10 other divs are in here)----------
 </div>


</div>
jQuery(document).ready(function( $ ) {
  $('select').on('change', function() {
    if(document.getElementById('selectid').value == "hl") {
      var dList = $(".dbl");
      dList.sort(function(a, b){ return $(b).data("l-disc")-$(a).data("l-disc")});
      $("#list").html(dList);
    }
  })
});

您可以使用隐藏它

$(".dbl").not("[data-l-disc]").hide()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM