[英]How can I correctly discover and print the browser window width using JQuery? Why it can't work?
I am pretty new in JQuery and I have the following problem. 我在JQuery中还很陌生,但遇到以下问题。
My page have to be differently rendered if the browser have specific width (I need it to fix an issue on mobile devices that have a smaller screen) 如果浏览器具有特定的宽度,则必须以不同的方式呈现我的页面(我需要使用它来解决屏幕较小的移动设备上的问题)
So as first step I am trying to discover the browser window width (so if it is less to a specific value I consider that the page is showed into a smartphone and I fix my issue). 因此,作为第一步,我试图发现浏览器窗口的宽度(因此,如果宽度小于某个特定值,我会认为该页面已显示在智能手机中,并且我已解决了该问题)。
So I am trying to do something like this: 所以我正在尝试做这样的事情:
$(document).ready(function () {
var windowWidth = $(window).width();
alert(windowWidth);
}
I am trying into JSFiddle link 我正在尝试进入JSFiddle 链接
But it seems don't work because I don't see any output. 但这似乎不起作用,因为我看不到任何输出。 Why?
为什么? What is wrong?
怎么了? How can I correctly discover and print the browser windows width?
如何正确发现并打印浏览器窗口的宽度?
I think you are missing jquery
also add this in your head
tag. 我认为您缺少的
jquery
也将此添加到您的head
标签中。
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
You are missing )
add it at end of your code. 您缺少
)
将其添加到代码末尾。
$(document).ready(function () {
var windowWidth = $(window).width();
alert(windowWidth);
});
Hello AndreaNobili I have updated yourjsFiddle here 您好AndreaNobili我在这里更新了yourjsFiddle
https://jsfiddle.net/ayjq1ke0/1/ https://jsfiddle.net/ayjq1ke0/1/
Basically you didn't click your document ready function. 基本上,您没有单击文档准备功能。
you just needed );
您只需要
);
at the end of your code for it to work :) oohh Also in jsFiddle you need to select what plugin to load, on your jsFiddle you didn't have jQuery selected, so I have added it for ya 在代码的末尾以使其起作用:)哦,在jsFiddle中,您还需要选择要加载的插件,在jsFiddle中,您没有选择jQuery,所以我为它添加了它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.