jquery - jQuery 中的目标 @import 没有 ID 也没有其他属性

标签 jquery

我有这个 jQuery 代码:

$(document).ready(function() {

    function adjustStyle(width) {
        width = parseInt(width);
        if (width < 701) {
            $(('style[type="text/css"]).attr([href=*400*]')).text('@import url("css/styles_400.css");');
        } else if ((width >= 701) && (width < 900)) {
            $(('style[type="text/css"].attr([href=*400*]')).text('@import url("css/styles_800.css");');
        } else {
           $(('style[type="text/css"].attr([href=*400*]')).text('@import url("css/styles_normal.css");');
        }
    }

    $(function() {
        adjustStyle($(this).width());
        $(window).resize(function() {
            adjustStyle($(this).width());
        });
    });
});

这是一个风格切换器。 I get this code from here ,但我做了一些修改,因为我正在制作的门户网站将合并服务器中的所有 css,因此,我必须 @import 样式表以仅针对一个 css 并正确更改它。

我无法为导入的CSS提供标题或ID(它是动态的),我尝试了上面的代码,因为我唯一的代码是:

  <style type="text/css">@import url('css/styles_400.css');</style>

如您所见,我唯一的线索就是 URL。

我如何定位这个 css? 我对 jQuery 的技能很少,而且我很确定我的代码做错了......但找不到适当的方法来瞄准它。

为什么我使用这个旧的样式切换器而不是媒体查询?因为我需要它用于 IE7(客户要求 - 我确信他们在星期六狩猎猛犸象),他们不喜欢 respond.js 和 css3-mediaqueries,因为它们会在 Plone 中引起奇怪的错误。

最佳答案

最后,经过几个小时的研究,我找到了解决方案:

$(文档).ready(function() {

function adjustStyle(width) {
    width = parseInt(width);
    if (width < 701) {
        $('style:contains("styles")').text('@import url("css/styles_400.css");');
    } else if ((width >= 701) && (width < 900)) {
        $('style:contains("styles")').text('@import url("css/styles_800.css");');
    } else {
       $('style:contains("styles")').text('@import url("css/styles_normal.css");');
    }
}

$(function() {
    adjustStyle($(this).width());
    $(window).resize(function() {
        adjustStyle($(this).width());
    });
});

});

它类似于jk.代码,我删除了.html方法,因为这样我就无法正确定位CSS,而是所有@import CSS。

此外,我还更改了关键字,因为当页面加载“styles_800”时,其他地方没有“400'”字符串......

关于jquery - jQuery 中的目标 @import 没有 ID 也没有其他属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17069626/

相关文章:

javascript - 单选按钮未正确验证

javascript - 使用 AJAX 请求重新加载整个页面并更改 GET 参数

javascript - 为什么人们使用 jQuery 进行基本操作?

jquery - 使用 jquery 提取字符串值

javascript - 单击jquery隐藏div

javascript - 冲动在线动画

javascript - jQuery 动画重叠问题

javascript - 使用 PHP 和 Jquery Ajax 进行输入验证

JavaScript OnClick() 启动卡拉 OK 文本?

javascript - 是否可以在 CSS 中使用三元运算符?