开发者

jQuery Find element else do nothing?

I am trying to use jQuery to basically wrap a bunch开发者_高级运维 of CSS modifications via jQuery but on pages where the IDs or Classes dont exist I get errors ? Like

jQuery(".class").css(random_stuff) is not a function

Any ideas what I can do to either find the elements and do nothing or ?


In this case it looks like the jQuery library isn't being included correctly.

If jQuery finds nothing matching your selector, nothing will happen because it didn't find any elements to perform the action on, this is the default behavior.


Sounds like you are missing a reference to jQuery on those pages. jQuery only performs an action on the matched selection...it will not throw an error if nothing matches.


if(jQuery(".class").length)
{
  jQuery(".class").css(random_stuff);
}

per Jquery Faq

You can use the length property of the jQuery collection returned by your selector:


You can check the return value's length property:

var myElement = $('.class');
if (myElement.length > 0)
    myElement.css(random_stuff);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜