<style>.main{left:100px;height:200px}</style>$('<style>').appendTo($('head'));這樣操作發生錯誤
2 回答

鴻蒙傳說
TA貢獻1865條經驗 獲得超7個贊
style也可以看做普通的HTML節點,因此其創建方法與jQuery創建普通元素的方法相同,關鍵代碼:
1 | $( "<style></style>" ).text( "div#test{color:red;}" ).appendTo($( "head" )); |
下面實例演示——點擊按鈕創建新的style樣式:
1、HTML結構
12 | < div id = "test" >這是示例的DIV</ div > < input type = "button" value="創建新的<style>"> |
2、jQuery代碼
123456 | $( function (){ $( "input[type='button']" ).click( function () { $( "<style id='test_style'></style>" ).text( "div#test{color:red;}" ).appendTo($( "head" )); $( "div#test" ).html($( "div#test" ).text() + "" + $( "#test_style" ).text()); }); }); |
3、效果演示

阿波羅的戰車
TA貢獻1862條經驗 獲得超6個贊
1.
$("head").css(
"left": "100px"
, "height": "200px"
)
2.
$("head").addClass("className")
- 2 回答
- 0 關注
- 376 瀏覽
添加回答
舉報
0/150
提交
取消