在CSS裡寫複雜的JavaScript指令碼_javascript技巧

來源:互聯網
上載者:User

因為"{"和"}"和CSS裡的 "{"與"}"衝突.
好像也不能用引號,具體沒有測試.
這樣一來,在CSS裡寫指令碼就不具備靈活了,但是如果想寫一個複雜的指令碼到CSS裡,該如何處理呢?是一個問題,經測試,可以先定義一個函數,然後在expression裡調用就行了,不過由於在CSS裡用了expression,所以,只能在IE裡運行.
一下是樣本,點擊代碼可運行.
<!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>Untitled Document</title> <script> ex=function(){ if(document.getElementById("dg")!=null){ return document.getElemntById("dg").offsetWidth; }else{ return 500; } } </script> <style> .aa { height:100px; width:expression(ex()); background-color: #00FF00; } </style> </head> <body> <div class="aa"></div> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

相關文章

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.