简体   繁体   中英

Why isn't the test in my jQuery filter function working as expected?

I used a function in filter to check whether the display mode is block but it's not working.

Here's my code:

$("#wrap_element").find("*").filter(function(){
    return this.css("display")==="block";
}).css("background-color","red");

Thanks.

You have a console error that hints at the problem:

Uncaught TypeError: this.css is not a function

You need to use a jQuery object since you're calling a jQuery method on it:

return $(this).css("display")==="block";
// ----^^----^

Demo

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