简体   繁体   English

使用jQuery检查CSS媒体查询不起作用

[英]checking css media queries with jquery not working

I cant help but feel stupid. 我不禁感到愚蠢。 I have a simple setup which should be working but is not. 我有一个简单的设置,应该可以,但是不能。 Obviously im missing something simple. 显然我错过了一些简单的东西。 Im trying to get it to load on doc ready rather than using window scroll btw. 我试图让它加载到文档准备好,而不是使用窗口滚动顺便说一句。

why does this not work? 为什么这不起作用?

if ($(".container").css("width") == "100%"){
    alert("adfgf adg df afdgadg");
}

http://jsfiddle.net/bxLLH/2/ http://jsfiddle.net/bxLLH/2/

thanks in advance. 提前致谢。

jQuery seems to only return pixel widths rather than percentages. jQuery似乎只返回像素宽度,而不是百分比。 Try using 尝试使用

if ($('.container').width() === $('.container').parent().width())

That will check to be sure something is the same width as it's direct parent element. 这将检查以确保某些宽度与其直接父元素的宽度相同。

use width() : 使用width()

if ($(".container").width() == $(window).width()){
    alert("adfgf adg df afdgadg");
}

demo: http://jsfiddle.net/bxLLH/3/ 演示: http : //jsfiddle.net/bxLLH/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM