木子屋 Dnawo's BLOG

Js动态添加样式

👤 dnawo 📅 2008-12-10 👁 7868 👍 0 💬 0 🔄 来源:本站原创
<script type="text/javascript">
//添加页面样式
function addStyle(content)
{
    var style;
    if(document.all)    //IE
    {
        style = document.createStyleSheet();
        style.cssText = content;
    }
    else
    {
        style = document.createElement("style"); 
        style.type = "text/css"; 
        //style.innerHTML = content;//Safari、Chrome 下innerHTML只读
        style.textContent = content;
    }
    try{document.getElementsByTagName("head")[0].appendChild(style);}catch(e){}//IE Error:不支持此接口
}
//添加外部样式
function includeStyle(url)
{
    var style = document.createElement("link");
    style.type = "text/css";
    style.setAttribute("rel","stylesheet");
    style.setAttribute("href",url);
    document.getElementsByTagName("head")[0].appendChild(style);
}
addStyle("#div1{background-color:#CC99FF;width:200px;height:100px;}");
includeStyle("http://www.mzwu.com/style/global.css");
</script>

参考文章

动态增加CSS,兼容任意浏览器:http://www.rainic.com/blog/index.php/183

评论(0)

暂无评论。

验证码
评论需审核通过后显示
← 上一篇 SQL Server2000创建视图 下一篇 → (function (){})()语句的优势