我有一个脚本,我在按下这样的按钮时加载一个CSS样式表:
link = "<link class='listcss' rel='stylesheet' href='http://....list.css'>";
$("head").append(link);
Run Code Online (Sandbox Code Playgroud)
然后我需要根据css的width和height属性做一些计算:
function CSSDone(){
if($(window).width()>640){
$(".grid-item").css({"height":$(".grid-item").width()*0.2});
$(".grid_item_img").css({"height":$(".grid-item").width()*0.2});
console.log("a");
}else{
$(".grid-item").css({"height":$(".grid-item").width()*0.33});
$(".grid_item_img").css({"height":$(".grid-item").width()*0.33});
console.log("b");
}
}
CSSDone();
Run Code Online (Sandbox Code Playgroud)
但是,如果我启动CSSDone(); 在添加样式表之后,计算在加载css之前发生.我疯狂搜索网页,但我尝试的一切都不起作用:
我试过这些选项:1)不起作用:
link.onload = function () {
CSSDone();
}
Run Code Online (Sandbox Code Playgroud)
2)不起作用:
if (link.addEventListener) {
link.addEventListener('load', function() {
CSSDone();
}, false);
}
Run Code Online (Sandbox Code Playgroud)
3)不起作用:
link.onreadystatechange = function() {
var state = link.readyState;
if (state === 'loaded' || state === 'complete') {
link.onreadystatechange = null;
CSSDone();
}
};
Run Code Online (Sandbox Code Playgroud)
4)不起作用:
$(window).load(function () {
CSSDone();
});
Run Code Online (Sandbox Code Playgroud)
5)不起作用:
$(window).bind("load", function() {
CSSDone();
});
Run Code Online (Sandbox Code Playgroud)
相信我.什么都行不通...... …
如何在html上使用div标签中的jquery更改样式表?或者更改样式表的jquery代码是什么?
在javascript中,我们使用以下代码:
<script type="text/javascript">
function changeStyle() {
document.getElementById('stylesheet').href = 'design4.css';
}
</script>
Run Code Online (Sandbox Code Playgroud)
有jQuery的方式吗?
我加载CSS:
$('head').append('<link rel="stylesheet" href="style2.css" type="text/css" />');
Run Code Online (Sandbox Code Playgroud)
它有效,但我有一个功能,需要在应用CSS后运行,我不想使用setInterval它.
我怎样才能做到这一点 ?