简体   繁体   中英

How to use jquery to select all elements that have two specific attributes

I have some markup where a lot of id's have an id attribute, as well as innerText. I want to select each of these elements, performing a function on the id.

How do I do that?

Something like this?

$('[id]:not(:empty)').each(function(i, el) {
  // do stuff
});

Give them a common class:

HTML

<div id="first" class="all"></div>
<div id="second" class="all"></div>
<div id="third" class="all"></div>

jQuery

$('div.all').each(function(index){
    processid(this.id);
});

First select by a regular ID selector and then loop over that selection by filtering .text() non-empty.

$("[id]").each(function() {
    if ($(this).text() != "") {
        // do stuff
    }
});

If you are talking about selecting elements whose id ( or some permutation of it ) is included in its text then

$('[id]').filter(function(){
  return $(this).text().indexOf( this.id ) >= 0; // the this.id should be altered to match the permutation you seek ..
}).css('color','red'); // turn those to red

After you comment to @lonesomeday ( at the question comments ) here is what to do ..

$('[id]').each(function(){
  processid(this.id);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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