javascript创建css样式
2013-03-13 12:36 youxin 阅读(787) 评论(0) 编辑 收藏 举报1:
var style = document.createElement(’link’);
style.href = ’style.css’;
style.rel = ’stylesheet’;
style.type = ‘text/css’;
document.getElementsByTagName(’HEAD’).item(0).appendChild(style);
2:
document.createStyleSheet().cssText = '标签{color:red;' +
// 这个注释只在当前JS中帮助理解,并不会写入CSS中
'width:300px;height:150px}' +
'.类名{……}' +
'#ID们{……}'
;
3;
完善一点的方法,防止重复添加,可以通过添加样式表ID并对其判断来实现:
if (!document.styleSheets['要建立的样式表ID如theforever']) { //先检查要建立的样式表ID是否存在,防止重复添加
var ss = document.createStyleSheet();
ss.owningElement.id = '要建立的样式表ID如theforever';
ss.cssText = '标签{display:inline-block;overflow:hidden;' +
// 这个注释只在当前JS中帮助理解,并不会写入CSS中
'text-align:left;width:300px;height:150px}' +
'.类名{……}' +
'#ID们{……}'
;
}
document.styleSheets:Returns a list of stylesheet objects for stylesheets explicitly linked into or embedded in a document.
The createStyleSheet method is only supported by Internet Explorer. In other browsers, use the createElement method to create a new link orstyle element, and the insertBefore or appendChild method to insert it into the document. See the examples below for details.
document.createStyleSheet('styles.css');
http://msdn.microsoft.com/en-us/library/ie/ms531194(v=vs.85).aspx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!