简体   繁体   English

如何使用JQuery正确发现和打印浏览器窗口宽度? 为什么它不起作用?

[英]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);
});

Fiddle: Demo here 小提琴: 这里演示

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.

相关问题 为什么我无法使用 JQuery 2 显示浏览器版本? - Why I can't show the browser version using JQuery 2? 我如何使用javascript或php从Web浏览器中打印页面或页面的一部分而无需打印窗口 - how i can print a page or part of the page from web browser without print window using javascript or php 无法使用jQuery从JSON数组正确打印数据 - Can't correctly print data from JSON array using jQuery 为什么我不能使该实体代码在浏览器中正确显示? - Why can't I get this entity code to display correctly in a browser? 使用jQuery,如何获取图片以全屏显示浏览器的高度和宽度? - Using jQuery, how can I get an image to open full screen to the browser's height and width? 如果未使用window.open()选择页面,如何使用jQuery关闭浏览器/页面? - How can I close a browser/page using jQuery if the page was not opned using window.open()? 如何使用JQuery进行打印 - How can I print using JQuery 为什么我不能使用jQuery的.keypress()、. keydown()和.keyup()事件正确比较两个字符串? - Why I can't compare two strings correctly using jQuery's .keypress(), .keydown() and .keyup() events? 3秒钟后如何使用jQuery或Javascript将浏览器置于所有其他窗口或浏览器的前面/焦点? - How can I bring browser to front/ in focus of all other window or browsers after 3 seconds using jQuery or Javascript? 如何使用javascript / jquery检查鼠标是否退出浏览器窗口? - How can I check if the mouse exited the browser window using javascript/jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM