如果屏幕窄于 1024px,则隐藏 DIV
我发现(从这个问题 - 如果屏幕小于某个值,则隐藏 div width) 这段代码
$(document).ready(function () {
if (screen.width < 1024) {
$("#floatdiv").hide();
}
else {
$("#floatdiv").show();
}
});
唯一的问题是,我似乎无法让代码工作,我只需要编码即可为 IE 工作,我将在其他(较新的)浏览器中使用媒体查询。 关于我哪里出错的任何提示/提示?
到目前为止我已经
然后在该 div 的末尾(关闭的地方)我有
<!--[if lt IE 9]>
<script type="text/javascript" src="http://dl.dropbox.com/u/17087195/website/sidebar_size.js"></script>
<![endif]-->
在我的标题中我有 <脚本类型='text/javascript' src='http://www.itsdaniel0.com/wp-includes/js/jquery/jquery.js?ver=1.4.4'> ;
我仍然无法让代码工作(在 IE8 中测试) 我还有什么地方出错了吗?
更新 我确实链接了另一段 jQuery,这是否会导致问题? 下面的完整代码
<div id="floatdiv">
<div class="floating-menu">
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.itsdaniel0.com%2F2011%2F03%2Funicorns-are-cool%2F&layout=box_count&show_faces=true&width=55&action=like&colorscheme=light&height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:55px; height:65px;" allowTransparency="true"></iframe>
<br /><br /><a href="http://twitter.com/share?url=http%3A%2F%2Fid0.me%2Fj0&counturl=http://www.itsdaniel0.com/2011/03/unicorns-are-cool/" class="twitter-share-button" data-text="Unicorns Are Cool" data-count="vertical" data-via="itsdaniel0 #itsdaniel0">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<br /><br />
<script src="http://widgets.fbshare.me/files/fbshare.js"></script>
</div>
</div>
<script type="text/javascript" src="http://dl.dropbox.com/u/17087195/website/sidebar.js"></script>
<!--[if lt IE 9]>
<script type="text/javascript" src="http://dl.dropbox.com/u/17087195/website/sidebar_size.js"></script>
<![endif]-->
这是错误消息
网页错误详细信息
用户代理:Mozilla/4.0(兼容; MSIE 7.0; Windows NT 5.1;三叉戟/4.0; chromeframe/10.0.648.133; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; OfficeLiveConnector.1.5; OfficeLivePatch.1.3; .NET4.0C; .NET4.0E; InfoPath.2) 时间戳:星期六, 2011 年 3 月 12 日 11:31:32 世界标准时间
消息:期望的标识符,字符串 或数字 行:140 字符:1 代码:0 网址: www.itsdaniel0.com/2011/03/unicorns-are-cool/
消息:对象不支持此功能 属性或方法 行:16 字符:1 代码:0 URI: dl.dropbox.com/u/17087195/website/sidebar_size.js
消息:'twttr.anywhere._instances' 为 null 或不是对象 Line: 1 Char: 5207 代码:0 URI: platform.twitter.com/anywhere.js?id=6vIPELyEeU5vcSc3c0Q5w&v=1
消息:'twttr.anywhere._instances' 为 null 或不是对象 Line: 1 Char: 5207 代码:0 URI: platform.twitter.com/anywhere.js?id=6vIPELyEeU5vcSc3c0Q5w&v=1
由于“低代表”错误而从 URL 中删除了 http
I found (from this question - Hide div if screen is smaller than a certain width) this piece of coding
$(document).ready(function () {
if (screen.width < 1024) {
$("#floatdiv").hide();
}
else {
$("#floatdiv").show();
}
});
The only problem is, I cannot seem to get the code to work, I only need to code to work for IE, I'm going to use Media Queries for other (newer) browsers.
Any hints/tips on where I'm going wrong?
So far I have<div id="floatdiv">
Then at the end of that div (where is closes) I have
<!--[if lt IE 9]>
<script type="text/javascript" src="http://dl.dropbox.com/u/17087195/website/sidebar_size.js"></script>
<![endif]-->
In my header I have<script type='text/javascript' src='http://www.itsdaniel0.com/wp-includes/js/jquery/jquery.js?ver=1.4.4'></script>
And I still cannot get the code to work (testing in IE8)
Am I still going wrong somewhere?
Update I do have another piece of jQuery linked, could this be causing the issue? Here is the full piece of coding below
<div id="floatdiv">
<div class="floating-menu">
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.itsdaniel0.com%2F2011%2F03%2Funicorns-are-cool%2F&layout=box_count&show_faces=true&width=55&action=like&colorscheme=light&height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:55px; height:65px;" allowTransparency="true"></iframe>
<br /><br /><a href="http://twitter.com/share?url=http%3A%2F%2Fid0.me%2Fj0&counturl=http://www.itsdaniel0.com/2011/03/unicorns-are-cool/" class="twitter-share-button" data-text="Unicorns Are Cool" data-count="vertical" data-via="itsdaniel0 #itsdaniel0">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<br /><br />
<script src="http://widgets.fbshare.me/files/fbshare.js"></script>
</div>
</div>
<script type="text/javascript" src="http://dl.dropbox.com/u/17087195/website/sidebar.js"></script>
<!--[if lt IE 9]>
<script type="text/javascript" src="http://dl.dropbox.com/u/17087195/website/sidebar_size.js"></script>
<![endif]-->
Error Message
Webpage error details
User Agent: Mozilla/4.0 (compatible;
MSIE 7.0; Windows NT 5.1; Trident/4.0;
chromeframe/10.0.648.133; .NET CLR
1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; OfficeLiveConnector.1.5;
OfficeLivePatch.1.3; .NET4.0C;
.NET4.0E; InfoPath.2) Timestamp: Sat,
12 Mar 2011 11:31:32 UTCMessage: Expected identifier, string
or number Line: 140 Char: 1 Code: 0
URI:
www.itsdaniel0.com/2011/03/unicorns-are-cool/Message: Object doesn't support this
property or method Line: 16 Char: 1
Code: 0 URI:
dl.dropbox.com/u/17087195/website/sidebar_size.jsMessage: 'twttr.anywhere._instances'
is null or not an object Line: 1 Char:
5207 Code: 0 URI:
platform.twitter.com/anywhere.js?id=6vIPELyEeU5vcSc3c0Q5w&v=1Message: 'twttr.anywhere._instances'
is null or not an object Line: 1 Char:
5207 Code: 0 URI:
platform.twitter.com/anywhere.js?id=6vIPELyEeU5vcSc3c0Q5w&v=1
Removed http from URLs due to "low rep" error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 JQUERY 的旧答案:
编辑:请注意,现在对 css3 媒体查询有更多的跨浏览器支持,使用它们而不是 javascript 会更有效。
使用 CSS。
请注意,在大多数现代浏览器中,您还可以使用 window.matchMedia 在 javascript 中运行媒体查询
OLD ANSWER USING JQUERY:
EDIT: Please note that now there is much more cross browser support for css3 media queries it would be much more effective to use those rather than javascript.
USING CSS.
Note that in most modern browsers you can also run media queries in javascript using window.matchMedia
您需要设置屏幕元素:
例如:
you need to set the screen element:
for example:
获胜的媒体查询
如果浏览器窗口处于特定宽度,如何使 div 不显示?
Media queries for the win
How do I make a div not display, if the browser window is at a certain width?