目标@import在jQuery中,没有ID,没有其他属性 [英] Target @import in jQuery with no ID and no other attributes
问题描述
我有这个jQuery代码:
I have this jQuery code:
$(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());
});
});
});
这是一个样式切换器. 我从这里获得了这段代码,但是由于我的网络门户网站,我做了一些修改m make将合并服务器中的所有css,因此,我必须@import样式表以仅针对一个css为目标并进行正确更改.
It's a styleswitcher. I get this code from here, but I have modificated a little because the webportal I'm making will merge all css in the server, so, I must @import the stylesheet to target only one css and change it properly.
我不能给导入的CSS加上标题或ID(它是动态的),我尝试了上面的代码,因为我仅有的代码是:
I can't give a title or an ID to the imported css (it's dynamic), and I tried the above code because the only code I have is:
<style type="text/css">@import url('css/styles_400.css');</style>
如您所见,我唯一的线索就是URL.
As you see, the only clue I have is the URL.
如何定位此CSS?我在jQuery中的技能很少,而且我很确定自己的代码做错了什么...但是找不到合适的方法来定位它.
How can I target this css? I have very little skills in jQuery, and I'm pretty sure that I'm doing something wrong with my code... but can't find the appropiate method to target it.
为什么我要使用这个旧的styleswitcher而不是mediaqueries?,因为我需要IE7 (客户要求-我确定他们在星期六搜索猛mm象),他们不喜欢response.js和css3-mediaqueries,因为它们会在Plone中引起奇怪的错误.
Why I'm using this old styleswitcher instead of mediaqueries? Because I need it for IE7 (client requirements - I'm sure they hunting mammoths on saturdays), and they don't like both respond.js and css3-mediaqueries because are causing weird errors in Plone.
推荐答案
最后,在玩了几个小时之后,我找到了解决方法:
Finally, and after some hours playing around, I find a solution:
$(document).ready(function(){
$(document).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.
It's similar to jk. code, I deleted .html
method because with that, I can't target properly the CSS, but all @import CSS's.
我还更改了关键字,因为当页面加载"styles_800"时,其他位置都没有"400'"字符串....
Also, I have changed the keyword, because when the page loads "styles_800" there's no "400'" string elsewhere....
这篇关于目标@import在jQuery中,没有ID,没有其他属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!