I want to apply CSS only to div tags with  

Asked 1 years ago, Updated 1 years ago, 35 views

I'm a beginner at jQuery.

If   is included in the div, I would like to add css to the div.
The code below does not work.
Please tell me how to write it.

$(function(){
 if($('.pf-list') .has(' ')}
 $(this).css({
    backgroundColor: '#ccc';
  });
 }
});

jquery

2022-09-29 22:28

2 Answers

A character reference nbsp corresponds to U+00A0 (specification, reference).
From this, you can select elements using :contains()Selector.

$('div:contains("\u00A0")').css({backgroundColor:'#ccc'});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>apple</div>
<div>banana</div>
<div>che&nbsp;rry</div>
<div>durian</div> 


2022-09-29 22:28

Normally, you can use the following API for text selectors, but
https://api.jquery.com/contains-selector/

Only &nbsp; can be used unless the text is grep directly.
https://stackoverflow.com/questions/29275742/can-i-select-an-element-that-contains-nbsp-within-its-html

$(function(){
  varregEx=/&nbsp;/gm;
  values = $(".pf-list").filter(function(index,ele){
     if(regEx.test($(ele).html())){
        $(ele).css("background-color", '#ccc');
    }
  });   
});


2022-09-29 22:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.