简体   繁体   English

在JavaScript中查找所有没有data-属性的元素

[英]Find all elements in javascript that do not have a data- attribute

I need to find all elements in $('.post-content') that do not have data attributes. 我需要在$('.post-content')中找到所有没有数据属性的元素。 I tried to say something like if (!$('.post-content p').attr('data-example')) { ... } 我试图说类似if (!$('.post-content p').attr('data-example')) { ... }

But obviously that does not work. 但是显然那是行不通的。 So how would I do the following: 因此,我将如何执行以下操作:

Find all p tags that belong to .post-content who do NOT have a data-attribute and remove them. 查找属于.post-content且没有数据属性的所有p标签,然后将其删除。

I think its very straight forward, but all the stack questions and docs, and jquery and the internet just has "how to add, how to find, how to add a value - to the data attribute" 我认为它非常简单,但是所有堆栈问题和文档,jquery和互联网都只有“如何添加,如何查找,如何向数据属性添加值”

So I was not completely clear, my apologies. 抱歉,我并不清楚。 In the example: 在示例中:

<div class="post-content">
  <p data-attribute="foo">
    <span data-attribute="boo">
     <p>...</p>
    </span>
  </p>
  <p> ... </p>
</div>

I do not want nested p tags, like the one you see here, where there is a nested p tag under the p tag that has a data attribute, to be removed. 我不希望删除嵌套的p标签(如您在此处看到的那样),在p标签下具有数据属性的嵌套p标签将被删除。 Just top level p tags that do not have a data attribute. 只是没有数据属性的顶级p标签。

The solutions provided tend to remove nested and top level. 提供的解决方案倾向于删除嵌套和顶层。

您可以使用.not()has属性选择器

$('.post-content p').not('[data-example]')

Not having a particular data attribute 没有特定的数据属性

You can use a combination of the :not() and [attribute] selectors ( fiddle ): 您可以结合使用:not()[attribute]选择器( fiddle ):

$(".post-content p:not([data-something])").remove();

Not having any data attributes 没有任何数据属性

A.: You could check if there's anything in the element's dataset using Object. keys() 答:您可以使用Object. keys()检查元素dataset是否存在任何内容Object. keys() Object. keys() ( fiddle ): Object. keys()小提琴 ):

$(".post-content p").filter(function(){
   return Object.keys(this.dataset).length === 0;
}).remove();

... or you could use for...in : ...或者您可以for...in

$(".post-content p").filter(function(){
   for (var prop in this.dataset) {
       return false;
   }
   return true;
}).remove();

Note: Depending upon the browsers you need to support, dataset may not be available . 注意:根据您需要支持的浏览器, dataset 可能不可用

B.: You could simply iterate over the element's attributes ( fiddle ): B .:您可以简单地遍历元素的attributesfiddle ):

$(".post-content p").filter(function(){
    return ![].some.call(this.attributes, function(attr){
        return /^data-/i.test(attr.name);
    });
}).remove();

See also: filter() and Array.prototype. some() 另请参见: filter()Array.prototype. some() Array.prototype. some()

Do you mean a specific data attribute, or just any data attribute (eg data-a, data-b, ... data-z, data-aa, data-ab, ...)? 您是指特定的数据属性,还是仅是任何数据属性(例如,数据-a,数据-b,...数据-z,数据-aa,数据-ab,...)?

If you mean a specific attribute then something like this should work: 如果您指的是特定属性,则应执行以下操作:

$(".post-content p:not([data-example]").remove()

But if you mean ANY data attribute, that's a whole 'nother thing. 但是,如果您指的是ANY数据属性,那完全是另一回事。 You'd have to find all the potential elements, then enumerate each of their attribute collections (eg $foo[0].attributes) to see if any of their names start with "data-". 您必须找到所有可能的元素,然后枚举它们的每个属性集合(例如$ foo [0] .attributes),以查看它们的名称是否以“ data-”开头。 If you have to do that I hope you don't have a lot of elements, it won't be very fast. 如果您必须这样做,我希望您没有太多的内容,那就不会很快。 Perhaps you could explain what the problem you're trying to solve is as there may be a better way to do it than this. 也许您可以解释您要解决的问题是什么,因为可能有比这更好的方法了。 Such as keeping a known specific attribute on those elements. 例如在这些元素上保留已知的特定属性。

You can either use the .dataset property (only works in newer browers) or you can use the .attributes property and look for attributes that start with "data-" . 您可以使用.dataset属性(仅适用于较新的浏览器),也可以使用.attributes属性并查找以"data-"开头的属性。

Here's a version that use the attributes list and will work in a wide range of browsers (doesn't require support for .dataset ): 这是一个使用attributes列表的版本,可以在多种浏览器中使用(不需要支持.dataset ):

$(".post-content").find("*").filter(function() {
    var attrs = this.attributes;
    regex = /^data-/;
    for (var i = 0; i < attrs; i++) {
        if (regex.test(attrs[i].name)) {
            // found a "data-xxxx" attribute so return false to avoid this element
            return false;
        }
    }
    // did not find any "data-xxxx" attributes so return true to include this element
    return true;
}).remove();

Your question wasn't entirely clear on one point. 您的问题在一点上还不是很清楚。 This code examines all descendants of .post-content . 此代码检查.post-content所有后代。 If you wanted to narrow it to only certain types of objects in that hierarchy, you can change the initial selector to narrow it. 如果只想将其范围缩小到该层次结构中的某些类型的对象,则可以更改初始选择器以使其缩小。

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

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