zoukankan      html  css  js  c++  java
  • htc使用方式(转)

    一.htc的两种使用方式:
    1. 关联行为(Attach Behavior): IE 5.0以上支持, htc 技术出现的初衷. 主要目的是把对象复杂的样式变化包装起来,替代 javascript + css.
    2. 元素行为(Element Behavior): IE 5.5以上支持, htc 技术的高级应用. 主要目的是用htc创建html文件中的自定义标签(相当于jsp技术中自定义标签).

    二.关联行为示例 (在Windows Xp, IE6.0中测试)

    1. 编写.htc文件. (style.htc)

          <public:attach event="onmouseover" onevent="f_mouse_over()"/>

          <public:attach event="onmouseout" onevent="f_mouse_out()"/>

          <public:attach event="onclick" onevent="f_click()"/>

          <public:method name="makeDisplay()"/>

          <public:method name="makeUnDisplay()"/>

          <script>

             function f_mouse_over(){ //使字体显红色,位置右移5px

               element.style.color="red";

               element.style.posLeft+=5;

             }

             function f_mouse_out() { //使字体显蓝色,位置左移5px

               element.style.color="blue";

               element.style.posLeft-=5;

             }

             function f_click(){  //点击鼠标后变成辉光显示,可用onmousedown,onmouseup事件细化处理

               element.style.color="red";

               element.style.filter="glow(color=red,strength=2)";

             }

             function makeDisplay(){    //使该对象可见

               element.style.display="block";

             }

             function makeUnDisplay(){  //使该对象不可见

               element.style.display="none";

             }

           </script>

        2.编写 html文件 (test.html)

           <html>

              <head>

                <style>

                   .testStyle{

                       behavior:url(style.htc);

                   }

                 </style>

                </head>

                <body>

                    <div style="200px;height:200px" class="testStyle" id="testDiv">Test Text;Please move mouse on this area!</div><br/>

                    <button onclick="testDiv.makeDisplay();" name="display"/><button onclick="testDiv.makeUnDisplay();" name="no display"/>

                  </body>

                </html>

          3.说明: 理论上htc文件里可以用className修改对象的样式,用的是html里的样式,htc里面用style定义的样式不能用. 但是,修改了className后可能对象就不再与本htc样式关联,本htc样式对该对象就没有作用了.   

    三. 元素行为示例 (WindowsXp, IE6.0上测试)

    1. 编写.htc文件 (element.htc)

        

    <public:component tagName="loginForm">

     <public:property name="userName" value="用户名"/>

      <public:property name="password" value="密码"/>

      <public:property name="action"/>

      <public:property name="firstStyle"/>  <!--用户名输入框的样式 -->

      <public:property name="secondStyle"/> 

      <public:attach event="oncontentready" onevent="init()"/>

      <public:method name="close"/>

      <public:defaults tabStop=true  contentEditable=false canHaveHTML=true viewInheritStyle=false viewMasterTab=false/>

    </public:component>

        <style>

           .name{
             background-color:red;
           }
           .pass{
             background-color:blue;
           }

        </style>

          <script>       
         var _table;       

             function init(){

               if (typeof(action)=="undefined" || action==null){

                 alert("Action property is missing!");

                 return;

               }//action 属性必须
               var _form=document.createElement("<form></form>");
               _form.action=action;
              
               _table=document.createElement("<table></table>");
               var oTr1=_table.insertRow();
               var oTd1=oTr1.insertCell();
               oTd1.innerText=userName+":";
               var oTd2=oTr1.insertCell();
               var _userName=document.createElement("<input type=text></input>");
               if (typeof(firstStyle)!="undefined" && firstStyle!=null){
                 _userName.className=firstStyle;
               }
               oTd2.appendChild(_userName);
               var oTr2=_table.insertRow();
               var oTd3=oTr2.insertCell();
               oTd3.innerText=password+":";
               var oTd4=oTr2.insertCell();
               var _password=document.createElement("<input type=password></input>");
               if (typeof(secondStyle)!="undefined" && secondStyle!=null){
                 _password.className=secondStyle;
               }
               oTd4.appendChild(_password);
               oTr3=_table.insertRow();
               oTd5=oTr3.insertCell();
               var _okBtn=document.createElement("<input type=submit></input>");
               oTd5.appendChild(_okBtn);
               oTd6=oTr3.insertCell();
               var _resetBtn=document.createElement("<input type=reset></input>");
               oTd6.appendChild(_resetBtn);
         
         _form.appendChild(_table);
         
         document.body.appendChild(_form);
              
              defaults.viewLink=document;

             }

             function close(){ //调用时删除自身
               var allEle=document.body.childNodes;
               for (var i=0;i<allEle.length;i++){
                 document.body.removeChild(allEle[i]);
               }
            }

          </script>

    <body></body>

         二.编写.html 文件 (test.html)

          <html>
           <head>
            <?xml:namespace prefix="custom"/>
            <?IMPORT NAMESPACE="custom" IMPLEMENTATION="element.htc"/>

            <style>
             .name{
               color:red;
             }
             .pass{
               color:blue;
             }
           </style>

          </head>
          <body>
            <custom:loginForm action="/login.do" id="aaa" firstStyle="name" secondStyle="pass"></custom:loginForm><br/>
            <input type="button" onclick="aaa.close();" value="删除登录框"/>
         </body>
         </html>
     

        3.说明: 在htc里用javascript生成的元素可以加在自定义控件中(用document.body.appendChild()),也可以直接加在主html文件中(用window.document.body.appendChild()). (htc文件和宿主html文件共用一个window对象,所以htc可以直接控制html文件)

        4.htc官方msdn文档:htc参考

  • 相关阅读:
    Channel 9视频整理【6】
    Channel 9视频整理【5】
    Channel 9视频整理【4】
    Channel 9视频整理【3】
    SQL_Server_2008完全学习之第二章管理工具
    SQL_Server_2008完全学习之第一章入门知识
    【转】大话模拟退火
    Unity使用DLL库
    常用纹理和纹理压缩格式
    Gamma空间和线性空间
  • 原文地址:https://www.cnblogs.com/swugogo/p/4630318.html
Copyright © 2011-2022 走看看