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';
});
}
});
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 rry</div>
<div>durian</div>
Normally, you can use the following API for text selectors, but
https://api.jquery.com/contains-selector/
Only
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=/ /gm;
values = $(".pf-list").filter(function(index,ele){
if(regEx.test($(ele).html())){
$(ele).css("background-color", '#ccc');
}
});
});
© 2024 OneMinuteCode. All rights reserved.