【问题标题】:Load jQuery file if window width is between values如果窗口宽度在值之间,则加载 jQuery 文件
【发布时间】:2013-12-05 11:42:48
【问题描述】:

我找到了很多解决方案,但没有一个有效..

我拥有的是这样的:

$(document).ready(function(){
    if ( $(window).width() > 480) {

    }
});

我想知道的是,当窗口宽度大于 480px 但小于 768px 时,如何将外部 js 文件加载到我的 html 头中。我尝试了“加载”和“写入”,但都没有工作。

提前致谢!

【问题讨论】:

    标签: jquery file load window width


    【解决方案1】:

    使用getScript

    $(document).ready(function(){
        if ( $(window).width() > 480 && $(window).width() < 768) {
            $.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {
              console.log( data ); // Data returned
              console.log( textStatus ); // Success
              console.log( jqxhr.status ); // 200
              console.log( "Load was performed." );
            });
        }
    });
    

    【讨论】:

    • 如果我用“js/jquery.zoom.js”替换“ajax/test.js”,它就不起作用。但是当我放置直接链接时: 它确实有效..(所有时间当然而不是窗口宽度之间..)
    • 您说要加载外部文件? URL 必须相对于执行代码的 html 文档
    • 请新建一个空白js文件,写“alert('loaded');”在其中,保存它并通过 getScript 加载这个 js 文件并告诉我会发生什么
    • 没有其他解决方案吗?
    【解决方案2】:
    // I loaded value between 901 to 1600 width, You can run with your min-width or max-width
    jQuery(document).ready(function($){ // Or else use with your requirment function (ex.$(window).on('load') or click function etc.)
    var windowsize = $(window).width();
    if ((windowsize >= 901) && (windowsize <= 1600)) {
           //Do Your Code Here... 
    }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 2014-01-23
      相关资源
      最近更新 更多