javascript - 使用 jQuery 根据浏览器宽度加载特定内容

标签 javascript jquery html css facebook

如果浏览器宽度大于 500,我正在尝试加载宽度为 466 的 Facebook 嵌入式帖子。如果浏览器宽度小于 500,将加载相同的 Facebook 帖子,但宽度为 350。 这是我的代码

<div class="col-lg-8 col-md-8 col-sm-12">
<script>
if($(window).width()<500)
{

//Load the post below
<div class="fb-post" data-href="https://www.facebook.com/TedTheStoner/photos/a.255068134610728.57957.255065497944325/671495962967941/?type=1" data-width="350"><div class="fb-xfbml-parse-ignore"><a href="https://www.facebook.com/TedTheStoner/photos/a.255068134610728.57957.255065497944325/671495962967941/?type=1">Post</a> by <a href="https://www.facebook.com/TedTheStoner">Ted The Stoner</a>.</div></div>

}
else
{
//Load this post 
<div class="fb-post" data-href="https://www.facebook.com/TedTheStoner/photos/a.255068134610728.57957.255065497944325/671495962967941/?type=1" data-width="466"><div class="fb-xfbml-parse-ignore"><a href="https://www.facebook.com/TedTheStoner/photos/a.255068134610728.57957.255065497944325/671495962967941/?type=1">Post</a> by <a href="https://www.facebook.com/TedTheStoner">Ted The Stoner</a>.</div></div>

我不知道该写什么而不是评论,这样脚本才能自动加载正确的版本。

编辑我更新的代码

<script>
$( document ).ready(function() {
if($(window).width()>500)
{
$('.fb-post').attr('data-width',466);
}
});
</script>
<div class="col-lg-8 col-md-8 col-sm-12">
<div class="fb-post" data-href="https://www.facebook.com/TedTheStoner/photos/a.255068134610728.57957.255065497944325/671495962967941/?type=1" data-width="350"><div class="fb-xfbml-parse-ignore"><a href="https://www.facebook.com/TedTheStoner/photos/a.255068134610728.57957.255065497944325/671495962967941/?type=1">Post</a> by <a href="https://www.facebook.com/TedTheStoner">Ted The Stoner</a>.</div></div>
</div>

页面仍以 350 宽度加载他发布的页面。这是页面 http://8mags.com/bored/facebook/facebookpastpost01.php

我的页面是响应式的,但 Facebook 帖子不是

最佳答案

你可以这样做:

<script>
    // You may or may not write the below code under the doc ready
    // Depends where and when you want the same to happen
    // For instance in the success function of any AJAX call or something like that
    $(document).ready(function () {
        $('.fb-post').attr('data-width',350);
    });
</script>
<div class="col-lg-8 col-md-8 col-sm-12">
    <div class="fb-post" data-href="https://www.facebook.com/TedTheStoner/photos/a.255068134610728.57957.255065497944325/671495962967941/?type=1" data-width="466">
        <div class="fb-xfbml-parse-ignore">
            <a href="https://www.facebook.com/TedTheStoner/photos/a.255068134610728.57957.255065497944325/671495962967941/?type=1">
                Post
            </a>
            &nbsp;by&nbsp; 
            <a href="https://www.facebook.com/TedTheStoner">
                Ted The Stoner
            </a>.
        </div>
    </div>
</div>

此外,如果您希望您的页面以响应方式工作,您可以使用 CSS 进行控制。为此,您可以引用 thisthis .有关更多信息,只需谷歌“@media CSS”

关于javascript - 使用 jQuery 根据浏览器宽度加载特定内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27637813/

相关文章:

javascript - 分机JS : generally frustrated with height and width issues

Javascript:如何取消 surroundContents 范围

javascript - Chaplin/Backbone 问题 - 将项目添加到集合时未触发 "Add"事件

javascript - 无法在 Django 中使用 jquery 禁用按钮

javascript向时间对象添加一分钟

jquery - 禁用直到动画完成

java - 重定向后 Servlet 名称仍在 Url 中(使用多页的 jQuery)

jquery - 强制 LI 出现在上一项的下一项上

html - 使用 css "display: table"属性复制的表格中的表格

javascript - 如何仅在使用 Javascript 将鼠标悬停在文本上时选择列表项