Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 加载DOM后更改样式表_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 加载DOM后更改样式表

Javascript 加载DOM后更改样式表,javascript,jquery,html,css,Javascript,Jquery,Html,Css,在我的HTML中有一个div: <div id="devicelayout"> Some text here </div> 加载页面后,如何更改CSS#devicelayout的规则 $(document).ready(function(){ $('#devicelayout').css("attributeName","attributeValue"); }) 这里有一些关于使用css函数的更多信息:请尝试以下代码: $(document).ready(

在我的HTML中有一个
div

<div id="devicelayout">
    Some text here
</div>
加载页面后,如何更改CSS
#devicelayout
的规则

$(document).ready(function(){
  $('#devicelayout').css("attributeName","attributeValue");
})
这里有一些关于使用css函数的更多信息:

请尝试以下代码:

$(document).ready(function(){
    $("#devicelayout").css("width","500px");
});

您可以使用jquery作为解决此问题的解决方案。。。在页面加载到css文件(例如devicelayout.css)后,将您希望的#devicelayout发生的所有css更改放入css文件中。然后在脚本标记中(当然,在包含jquery库之后)添加以下代码

$(document).ready(function(){ 
   if($("#devicelayout").size()>0){ /*Check if the id "#devicelayout" exists within your DOM*/

  $("head").append($("<link rel='stylesheet' href='devicelayout.css' type='text/css' />")); /*if true, the new <link> element is appended to the <head> element */   }
});
$(文档).ready(函数(){
if($(“#devicelayout”).size()>0{/*检查DOM中是否存在id“#devicelayout”*/
$(“head”).append($(“”);/*如果为true,新元素将被追加到元素*/}
});

这还允许您在页面加载后更改多个ID的css,而不仅仅是一个ID。如果您只想更改元素的css样式,则可以直接为其分配不同的类,或者使用JQuery更改元素的css属性,希望这对您有所帮助

$(“#设备布局”).css(“属性”、“值”)

$(“#设备布局”).addClass(“新类”)


或者,如果要更改css规则,请尝试在
document.styleSheets
对象中修改样式表规则。请参见更改规则?你能提供更多细节吗?我的意思是可以在类中定义规则,然后将该类添加到元素中。也可以直接在元素上放置样式。你确定你真的需要你想要的东西吗?你使用jquery还是普通javascript,正如我们从你添加的标记中所假设的那样?我想更改css选择器#devicelayoutCheck的定义,也许你会有一些想法。jquery或javascript两者都有
$(document).ready(function(){ 
   if($("#devicelayout").size()>0){ /*Check if the id "#devicelayout" exists within your DOM*/

  $("head").append($("<link rel='stylesheet' href='devicelayout.css' type='text/css' />")); /*if true, the new <link> element is appended to the <head> element */   }
});