• CSS中expression简介实现对象批量控制


    定义
      IE5及其以后版本支持在CSS中使用expression,用来把CSS属性和Javas cript表达式关联起来,这里的CSS属性可以是元素固有的属性,也可以是自定义属性。就是说CSS属性后面可以是一段Javas cript表达式,CSS属性的值等于Javas cript表达式计算的结果。 在表达式中可以直接引用元素自身的属性和方法,也可以使用其他浏览器对象。这个表达式就好像是在这个元素的一个成员函数中一样。

      给元素固有属性赋值

      例如,你可以依照浏览器的大小来安置一个元素的位置。

    #myDiv {
    position: absolute;
    100px;
    height: 100px;
    left: expression(document.body.offsetWidth - 110 + "px");
    top: expression(document.body.offsetHeight - 110 + "px");
    background: red;
    }

      给元素自定义属性赋值

      例如,消除页面上的链接虚线框。 通常的做法是:

    <a href="link1.htm" onfocus="this.blur()">link1</a>
    <a href="link2.htm" onfocus="this.blur()">link2</a>
    <a href="link3.htm" onfocus="this.blur()">link3</a>

      粗看或许还体现不出采用expression的优势,但如果你的页面上有几十甚至上百个链接,这时的你难道还会机械式地Ctrl+C,Ctrl+V么,何况两者一比较,哪个产生的冗余代码更多呢?

      采用expression的做法如下:

    <style type="text/css">
    a {star : expression(onfocus=this.blur)}
    </style>
    <a href="link1.htm">link1</a>
    <a href="link2.htm">link2</a>
    <a href="link3.htm">link3</a>

      说明:里面的star就是自己任意定义的属性,你可以随自己喜好另外定义,接着包含在expression()里的语句就是JS脚本,在自定义属性与expression之间可别忘了还有一个引号,因为实质还是CSS,所以放在style标签内,而非s cript内。OK,这样就很容易地用一句话实现了页面中的链接虚线框的消除。不过你先别得意,如果触发的特效是CSS的属性变化,那么出来的结果会跟你的本意有差别。例如你想随鼠标的移进移出而改变页面中的文本框颜色更改,你可能想当然的会认为应该写为

    <style type="text/css">
    input
    {star : expression(onmouseover=this.style.backgroundColor="#FF0000";
    onmouseout=this.style.backgroundColor="#FFFFFF")}
    </style>
    <style type="text/css">
    input {star : expression(onmouseover=this.style.backgroundColor="#FF0000";
    onmouseout=this.style.backgroundColor="#FFFFFF")}
    </style>
    <input type="text">
    <input type="text">
    <input type="text">

      可结果却是出现脚本出错,正确的写法应该把CSS样式的定义写进函数内,如下所示:

    <style type="text/css">
    input {star : expression(onmouseover=function()
    {this.style.backgroundColor="#FF0000"},
    onmouseout=function(){this.style.backgroundColor="#FFFFFF"}) }
    </style>
    <input type="text">
    <input type="text">
    <input type="text">

      注意

      不是非常需要,一般不建议使用expression,因为expression对浏览器资源要求比较高

    =======================================================================

    利用css里expression来实现界面对象的批量控制

    问题说明: 用过css样式我们就知道, 可以定义一批对象的class属性来指定同一个样式来统一界面. 但如何统一同类型的对象的事件? 比如:界面有无数个 <img src="**.jpg"> 如何实现鼠标经过此图片, 图片的src变成是**_over.jpg?
    解决方法: 使用css的expression方法,
    具体实现要看看.css的写法:
    /*替换图片CSS*/
    #imgScript {      /*这里使用对象ID来通配样式, 也可以定义一个css函数*/
    star:expression(
    onmouseover = function()
    {
    /*替换图片*/
    if(this.hover != null){
    this.name = this.src;
    this.src = this.src.replace(’.jpg’, ’_over.jpg’);
    this.HasChg = 1;
    }
    },
    onmouseout = function()
    {
    /*还原本来的图片*/
    if(this.HasChg != null){
    this.src = this.name;
    this.HasChg = null;
    }
    }
    )
    }/*end imgScript*/
    应用样式的img:
    <img src="a.jpg">
    请将鼠标放在a.jpg上看看效果

    =======================================================================

    可以把JavaScript代码,写在css里吗?

    没有做不到的,只有想不到的!我们可以先写好JS代码,然后再在CSS中调用它即可:

      首先写JS(javascritp):

    <script language="javascript">  
        
    //定义table的样式.cellSpacing,cellPadding   
    //borderColorLight       ,borderColorDark.   
        
    function       table3d(obj)       {   
          obj.border=1;   
          obj.cellSpacing=0;   
          obj.cellPadding=0;      
          obj.borderColorLight="#ffffff";   
          obj.borderColorDark="#ffffff";   
    }
        
    //定义td的样式.       bgColor   
    //borderColorLight,       borderColorDark   
        
    function       td3d(obj)       {   
          obj.bgColor="#0000ff";   
          obj.borderColorLight="#ffffff";   
          obj.borderColorDark="#eeeeee";   
    }   
    </script>   

      再写样式表:
        
    <style>   
    <!--定义样式-->   
    .table3d{wwwfan:expression(table3d(this))}   
    .td3d{wwwfan:expression(td3d(this))}   
    </style>

      在表格中应用样式:

    <table class=table3d> <!--应用table3d样式-->
    <tr align="center">
    <td width="100" class=td3d>立体</td> <!--应用td3d样式-->
    <td width="100" class=td3d>表格</td> <!--应用td3d样式-->
    </tr>
    </table>

      说明:

      .table3d{wwwfan:expression(table3d(this))}

      .table3d 定义一个class,不用我再罗嗦了吧!
    wwwfan 是自已定义的属性,可以用你的英文名任意取个名字!因为这是你自己的CSS啊!
    expression()里面的语句就是JavaScript. 一定很熟悉吧!
    table3d(this). 调用了前面写的函数。这个函数很简单,只是包含了对borderColorLight、borderColorDark、cellSpacing等的定义。

    ===============================================

    --------------------表格隔行换色一----------------------------

    <!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=gb2312" />
    <title>表格隔行换色二</title>
    <style type="text/css">
    <!--
    tr {
    background-color:expression((this.sectionRowIndex%2==0)?"red":"blue");
    }
    -->
    </style>
    </head>
    <body>
    </HEAD>
    <table>
    <tr>
    <td>第1行</td><td>第1行</td>
    </tr>
    <tr>
    <td>第2行</td><td>第2行</td>
    </tr>
    <tr>
    <td>第3行</td><td>第3行</td>
    </tr>
    <tr>
    <td>第4行</td><td>第4行</td>
    </tr>
    <tr>
    <td>第5行</td><td>第5行</td>
    </tr>
    </table>
    </body>
    </html>

    --------------------表格隔行换色二----------------------------

    <!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=gb2312" />
    <title>表格隔行换色一</title>
    <style type="text/css">
    <!--
    .DoubleColorTable tr {
    background-color:expression("#F1F8FF,#ffffff".split(",")[rowIndex%2])
    }
    -->
    </style>
    </head>

    <body>
    <table border="1" cellpadding="0" cellspacing="0" bordercolorlight="#6C7BA6" bordercolordark="#ffffff" bgcolor="#DEEFFF" class="DoubleColorTable">
        <tr>
          <td>123456789</td>
          <td>45873123456</td>
          <td>4587312345</td>
          <td>4587312345</td>
          <td>4587312345</td>
        </tr>
        <tr>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
          <td>&nbsp;123456789456123348</td>
          <td>&nbsp;23456789</td>
        </tr>
        <tr>
          <td>&nbsp;123456789</td>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
        </tr>
        <tr>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
        </tr>
        <tr>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
          <td>&nbsp;23456789</td>
        </tr>
    </table>
    </body>
    </html>

    --------------------表格隔行换色三----------------------------

    <!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=gb2312" />
    <style>   
    .db tr{   
    background-color:expression('#000000,#333333,#555555,#777777,#999999,#bbbbbb,#dddddd,#ffffff'.split(',')[rowIndex%8]);   
    }   
    </style>
    <title>表格隔行换色三</title>
    </head>

    <body>  
    <table width="100%" border="1" class="db">   
    <tr>   
    <td>aaaaaaaaaaaaaa</td>   
    </tr>   
    <tr>   
    <td>bbbbbbbbbbbbbbb</td>   
    </tr>   
    <tr>   
    <td>cccccccccccc</td>   
    </tr>   
    <tr>   
    <td>fffffffffffff</td>   
    </tr>   
    </table>   
        
    上面是每一行隔行换色每两行一循环,下面是每一行隔行换色,每八行一循环   
    </body>
    </html>

    --------------------表格隔行换色四----------------------------


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>表格隔行换色四</title>
    <style type="text/css" media="screen">
    <!-- /* PR-CSS */
    table {border-collapse:collapse;border:solid #999;border-1px 0 0 1px;}
    table td {border:solid #999;border-0 1px 1px 0;}
    .t1 {background-color:#fff;}/* 第一行的背景色 */
    .t2 {background-color:#eee;}/* 第二行的背景色 */
    .t3 {background-color:#ccc;}/* 鼠标经过时的背景色 */
    -->
    </style>
    </head>
    <body>
    <ul><li>11111111</li>
    <li>222222222</li>
    <li>3333333</li>
    <li>444444444</li>
    </ul>
    <script type="text/javascript">
    <!--
    var Ptr=document.getElementsByTagName("li");
    function $() {
          for (i=1;i<Ptr.length+1;i++) {
          Ptr[i-1].className = (i%2>0)?"t1":"t2";
          }
    }
    window.onload=$;
    for(var i=0;i<Ptr.length;i++) {
          Ptr[i].onmouseover=function(){
          this.tmpClass=this.className;
          this.className = "t3";
        
          };
          Ptr[i].onmouseout=function(){
          this.className=this.tmpClass;
          };
    }
    //-->
    </script>
    </body>
    </html>


  • 相关阅读:
    HTTP协议中常用相应的状态码总结
    mysql 用户管理
    史上最全的mysql聚合函数总结(与分组一起使用)
    jQuery+masonry实现瀑布流
    MySQL Workbench 导入导出乱码解决方法
    在Google Maps 上点击标签显示说明并保持不消失
    在Google Maps 上点击标签后显示说明
    如何在Google Maps 添加多个标记
    如何在 Google 地图中添加标记和说明
    Google Maps API3 之 Hello World
  • 原文地址:https://www.cnblogs.com/see7di/p/2239953.html
Copyright © 2020-2023  润新知