style對象的cssText方法,style對象csstext

來源:互聯網
上載者:User

style對象的cssText方法,style對象csstext

cssText 本質是什嗎?

  cssText 的本質就是設定 HTML 元素的 style 屬性值。

cssText 怎麼用?

domElement.style.cssText = "color:red; font-size:13px;";

cssText 傳回值是什嗎?

在某些瀏覽器中(比如 Chrome),你給他賦什麼值,它就返回什麼值。在 IE 中則比較痛苦,它會格式化輸出、會把屬性大寫、會改變屬性順序、會去掉最後一個分號,比如:

1 document.getElementById("d1").style.cssText = "color:red; font-size:13px;";2 alert(document.getElementById("d1").style.cssText);

在 IE 中值為:FONT-SIZE: 13px; COLOR: red

cssText的使用優勢

  一般情況下我們用js設定元素對象的樣式會使用這樣的形式:

    var element= document.getElementById(“id”);
    element.style.width=”20px”;
    element.style.height=”20px”;
    element.style.border=”solid 1px red”;

  樣式一多,代碼就很多;而且通過JS來覆寫對象的樣式是比較典型的一種銷毀原樣式並重建的過程,這種銷毀和重建,都會增加瀏覽器的開銷。

  js中有一個cssText的方法:

  domElement.style.cssText=”樣式”;  domElement.style.cssText=”width:20px;height:20px;border:solid 1px red;”;

  這樣就可以盡量避免頁面reflow,提高頁面效能。

  但是,這樣會有一個問題,會把原有的cssText清掉,比如原來的style中有’display:none;’,那麼執行完上面的JS後,display就被刪掉了。
  為瞭解決這個問題,可以採用cssText累加的方法:

  domElement.style.cssText += ‘;width:100px;height:100px;top:100px;left:100px;’

  再進一步,如果前面有樣式表檔案寫著 div { text-decoration:underline; },這個會被覆蓋嗎?不會!因為它不是直接作用於 HTML 元素的 style 屬性。

  具體案例分析:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>控制div屬性</title><style>#outer{width:500px;margin:0 auto;padding:0;text-align:center;}#div1{width:100px;height:100px;background:black;margin:10px auto;display:block;}</style><script>var changeStyle = function (elem, attr, value){    elem.style[attr] = value};window.onload = function (){    var oBtn = document.getElementsByTagName("input");    var oDiv = document.getElementById("div1");    var oAtt = ["width","height","background","display","display"];    var oVal = ["200px","200px","red","none","block"];     for (var i = 0; i < oBtn.length; i++)    {        oBtn[i].index = i;        oBtn[i].onclick = function ()        {            this.index == oBtn.length - 1 && (oDiv.style.cssText = "");            changeStyle(oDiv, oAtt[this.index], oVal[this.index])        }      }};</script></head><body><div id="outer"><input type="button" value="變寬" /><input type="button" value="變高" /><input type="button" value="變色" /><input type="button" value="隱藏" /><input type="button" value="重設" /><div id="div1"></div></div>  </body></html>

 本文是在學習了https://www.cnblogs.com/majingyi/p/6840818.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.