替换动态内容jQuery/javascript [英] Replacing dynamic content jQuery/javascript

查看:73
本文介绍了替换动态内容jQuery/javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用jQuery在字符串中进行动态替换.我已经读过关于在选择器中使用通配符的信息,但是我希望在字符串中使用通配符.

I need to do a dynamic replace in a string with jQuery. I have read about using wildcards in selectors, but I want it in a string.

考虑此HTML:

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

我在jQuery中具有此功能:

And I have this function in jQuery:

function adjustStyle(width) {
    width = parseInt(width);
    if (width < 701) {
        $('style:contains("MQ")').text(function () {
        return $(this).text().replace("MQ.css", "400MQ.css");
        });
    } else if ((width >= 701) && (width < 900)) {
        $('style:contains("MQ")').text(function () {
        return $(this).text().replace("MQ.css", "800MQ.css");
        });         
    } else {
       $('style:contains("MQ")').text(function () {
        return $(this).text().replace("MQ.css", "MQ.css");
        });         
    }
}

此函数是整个jQuery的一部分,该jQuery会根据屏幕尺寸更改@import的CSS.

This function is part of an entire jQuery that changes an @imported css depending of screen size.

错误

它可以工作,但是当我开始使用屏幕尺寸播放时,会得到类似以下内容:

It works, but when I start playing with the screen size I get something like:

@import url('css/styles800800800800400400400400400400400400400400MQ.css');


我不知道如何告诉jQuery也替换数字.
我想我需要像这样的东西:


I can't figure how can I tell jQuery to replace also the number.
I suppose that I need something like:

.replace("styles*MQ.css", "styles400MQ.css");


提示:

  • 我不使用媒体查询,因为我的客户是Fred Flinstone,他使用IE7,并且不希望使用pollyfills,因为额外的加载时间并因为它们是恶魔.
  • 页面中还有其他@imported CSS
  • 我不知道CSS的整个路径,也不知道文件夹的名称,这只是我的名字(这就是我要替换而不是写新CSS的相对路径位置的原因.)
  • 我可以更改任何CSS的名称.

推荐答案

代替.replace("MQ.css", "MQ.css");尝试类似

.replace(/\d*MQ\.css/, "MQ.css");
.replace(/\d*MQ\.css/, "400MQ.css");

这篇关于替换动态内容jQuery/javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆