用HTC文件,为按钮添加 link 和 target 属性

来源:互联网 发布:cmd命令大全网络 编辑:程序博客网 时间:2024/06/09 16:57
做WEB程序的时候经常需要用一个按钮来跳转到一个页面,或打开新窗口;由于按钮没有link属性,所以经常要写脚本来控制,感觉有些麻烦,最近看了一些关于HTC的文档,发现HTC可以为按钮添加属性,所以就写了个例子。
 
文件:test.htm
 
<link href="style.css" rel="stylesheet" type="text/css">
<input type=button link="http://www.sina.com.cn" value="打开新浪">
<input type=button link="http://www.sina.com.cn" value="新窗口打开新浪" target="_blank">
 
文件:style.css
 
Input{behavior:url('input.htc');}
文件:input.htc
 
<public:component>
<!--添加连接-->
<public:property name="link" value="" />
<!--添加是否在新窗口打开属性-->
<public:property name="target" value="" />
<script language=javascript>
if(this.onclick==null)
{
onclick=function()
{
  if (link!="")
  {
    if (target=="_blank")
    {
      window.open(link);
    }
     else
    {
      location.href=link;
    }
  }
};
};
</script>
</public:component>
 
把上面的代码分别保存,放在同一个目录下就可以了,以后只要引入了style.css,那么页面中的按钮就多了两个属性 link ,target。
如果你设置了按钮的onclick属性,那么link属性就不执行了,毕竟默认的属性优先吗。
原创粉丝点击