本文介绍了jQuery select元素,如果* any *属性匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个类似$(".author")的选择器,如果作者"是任何属性(而不仅仅是类)的值,它将选择一个元素.

I want a selector like $(".author") that will select an element if "author" is the value of any attribute, not just class.

例如selector.text()应该为以下每个页面提供"Emily Ekins":

E.g. selector.text() should give "Emily Ekins" for each of the following pages:

<a href="http://reason.com/people/emily-ekins" rel="author">Emily Ekins</a>

<bloop href="http://reason.com/people/emily-ekins" id="author">Emily Ekins</a>

<blah href="http://reason.com/people/emily-ekins" class="author">Emily Ekins</a>

<tag href="http://reason.com/people/emily-ekins" random="author">Emily Ekins</a>

有什么办法吗?

推荐答案

var elements = document.getElementsByTagName("*");
for(var i=0;i<elements.length;i++){
    var element = elements[i];
    var attr = element.attributes;
    for(var j=0;j<attr.length;j++){
        if(attr[j].nodeValue == 'author'){
            element.style.backgroundColor='red';
        }
    }
}

示例

哇!那花了一些时间.但这会检查任何给定元素每个单一属性,如果 any 属性为author,它将背景色更改为红色

Whew! That took some time. But this will check every single attribute of any given element, and if any attribute is author, it will change the background color to red.

这篇关于jQuery select元素,如果* any *属性匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 10:53