js操作建立和操作外部樣式的例子

來源:互聯網
上載者:User

標籤:style   http   os   io   cti   ar   html   代碼   

相容IE8及以上的IE瀏覽器
1. [代碼][HTML]代碼 
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <div id="test"></div>
    </body>
    <script src="../lib/jquery-1.8.3.min.js"></script>
    <script>
        /*
         * Firefox無 addRule,低版本IE無insertRule,insertRule是w3c標準
         * 此處insertRule -> addRule的代碼是因為inserterRule只有
         *2個參數。2個參數轉成3個參數狀況比較多
         */
        if(!CSSStyleSheet.prototype.insertRule){
            CSSStyleSheet.prototype.insertRule = function(style, index){
                if(!style){
                    return;
                }
                var style = style.split("{");
                if(style.length<2){
                    return;
                }
                this.addRule(style[0], style[1].split("}")[0], index);
            }
        }
         
        /*
         * Firefox無 removeRule,低版本IE無deleteRule
         * delete?Rule是w3c標準音效網
         */http://www.huiyi8.com/yinxiao/?
        if(!CSSStyleSheet.prototype.deleteRule){
            CSSStyleSheet.prototype.deleteRule = function(index){
                this.removeRule(index);
            }
        }
         
        /**
         * 建立一個外部樣式表
         */
        function sheet(appendTo){
            var style = document.createElement("style");
            style.type = ‘text/css‘;
            appendTo ? appendTo.append(style) : $("body").append(style);
            return style.sheet ? style.sheet : style.styleSheet;
        }
         
        var sheet = new sheet();
        sheet.insertRule("#test{width:100px;height:100px;background:red;border:5px solid green;}", 0);
        sheet.insertRule("#test{position:absolute;}", 1);
        sheet.insertRule("#test{position:absolute;}", 2);
         
        var mouseDown = false;
        var position;
        var style;
        var target = $("#test");
        var x,y;
        var rule;
        $("#test").mousedown(function(e){
            mouseDown = true;
            x = e.clientX;
            y = e.clientY;
            position = target.position();
        }).mousemove(function(e){
            if(mouseDown == true){
                rule = sheet.cssRules ? sheet.cssRules[2] : sheet.rules[2];
                rule.style.top = (position.top + e.clientY - y) + "px";
                rule.style.left = (position.left + e.clientX - x) + "px";
            }
        }).mouseup(function(){
            mouseDown = false;
        });
    </script>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.