最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery if statement depending on width in px - Stack Overflow

programmeradmin0浏览0评论

I'm quite new to jQuery, could someone please tell me if I have expressed the above 'if' statement correctly? I basically want something to run if the width of my variable equals 900px. My variable is var $brewapp = $('#brewapp');

Thanks.

if (($brewapp).width == '900px')
{
//what i want it to do
}

I'm quite new to jQuery, could someone please tell me if I have expressed the above 'if' statement correctly? I basically want something to run if the width of my variable equals 900px. My variable is var $brewapp = $('#brewapp');

Thanks.

if (($brewapp).width == '900px')
{
//what i want it to do
}
Share Improve this question asked May 26, 2011 at 9:12 LukeLuke 2053 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

.width is a function, that returns a number. So your test should look like this:

if ($brewapp.width() === 900)

I suggest the Mozilla Javascript docs if you want to deepen your JS knowledge.

You need to add only two characters:

if ($brewapp.width() == 900)
{
//what i want it to do
}

I guess your code should be:

if ($brewapp.width() == 900)
{
//what i want it to do
}

First width is a function, so use () and it returns an integer, so no need to append 'px'.

This is the jQuery way. Using the jQuery width() gives better results than using el.style.width (or what you intended to use).

发布评论

评论列表(0)

  1. 暂无评论