开发者

jQuery filter selector, is this right?

开发者 https://www.devze.com 2023-01-01 21:10 出处:网络
It seems to work ok but I don\'t know if this can be impoved on or not. I want to select any HTML tag that has a class of edit-text-NUM or edit-html-NUM and change the color of it. Here is what I am

It seems to work ok but I don't know if this can be impoved on or not.

I want to select any HTML tag that has a class of edit-text-NUM or edit-html-NUM and change the color of it. Here is what I am using...

jQuery(document).ready(function(){
    jQuery('*')
    .filter(function() {
        return this.clas开发者_高级运维sName.match(/edit-(text|html)-\d/);
    })
    .css({
        'color': '#ff0000'
    });
});

Does that look ok and is the regex ok?

*edit: Also is this efficient? I am aware that using jQuery('*') might be a bit of a hog if it's a large page. It only has to work from <body> down so maybe it could be changed?


jQuery(document).ready(function(){ 
jQuery('body *') 
.filter(function() { 
    return this.className.match(/edit-(text|html)-\d/); 
}) 
.css({ 
    'color': '#ff0000' 
}); 

});

should limit it to the document body


You can use:

$("[class^='edit-text-'], [class^='edit-html-']")

Note that this isn't exactly the same. For one, it will not work if you have multiple classes.

If you are cautious about performance, a much better solution is to add another class to all elements:

<em class="edit-text-44 edit-text">

Then, you can simply use:

$(".edit-text, .edit-html")
0

精彩评论

暂无评论...
验证码 换一张
取 消