jQuery if statement depending on width in px

二次信任 提交于 2020-01-02 06:58:39

问题


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
}

回答1:


.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.




回答2:


You need to add only two characters:

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



回答3:


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).



来源:https://stackoverflow.com/questions/6136193/jquery-if-statement-depending-on-width-in-px

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!