<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 关注
- 463 浏览
添加回答
举报
0/150
提交
取消

