欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用htc实现CHECKBOX控件

程序员文章站 2022-06-19 16:12:13
复制代码 代码如下:/* 描述:        checkbox控件 版本: &n...
复制代码 代码如下:

/*
描述:        checkbox控件
版本:        1.1
备注:        checkbox控件背后跟随的文字
            是获取checkbox对象的htc_mylabel来显示的
            更新添加indeter属性,用来增加不确定的选择
*/
<public:component>
<public:attach event="oncontentready" onevent="fninit()" />
<public:attach event="onpropertychange" onevent="fnpropertychange()" />
<script language="javascript">
var checkboximg=window.document.createelement("img");
imagearray=[];
imagearray["checkbox_false"]="checkbox_false.gif";
imagearray["checkbox_false_down"]="checkbox_false_down.gif";
imagearray["checkbox_false_over"]="checkbox_false_over.gif";
imagearray["checkbox_true"]="checkbox_true.gif";
imagearray["checkbox_true_down"]="checkbox_true_down.gif";
imagearray["checkbox_true_over"]="checkbox_true_over.gif";
imagearray["checkbox_indeter"]="checkbox_indeter.gif";
imagearray["checkbox_indeter_down"]="checkbox_indeter_down.gif";
imagearray["checkbox_indeter_over"]="checkbox_indeter_over.gif";
function preload(path,obj){
    for(i in obj){
        this[i]=new image();
        this[i].src=path+obj[i];
        }
    return this;
    }
preb=new preload("images/",imagearray);
function fninit(){
    var o=element;
    if(o.type=="checkbox"){
        var _table=window.document.createelement("table");
        _table.cellspacing="0px";
        _table.cellpadding="0px";
        _table.border="0px";
        if(o.disabled){
            _table.style.filter="alpha(opacity=50)";
            }
        else{
            _table.style.filter="";
            }
        _table.style.display="inline";
        var _tr=_table.insertrow();
        var _td=_tr.insertcell();
        if (o.checked){
            checkboximg.src=preb["checkbox_true"].src;
            }
        else{
            checkboximg.src=preb["checkbox_false"].src;
            }
        if(o.indeter=="true"){
            o.indeterminate="true";
            checkboximg.src=preb["checkbox_indeter"].src;
            }
        _td.appendchild(checkboximg);
        _td=_tr.insertcell();
        _td.style.verticalalign="bottom";
        if(o.htc_mylabel){
            _td.innerhtml="&nbsp<label style='cursor: hand'>"+o.htc_mylabel+"</label>";
            }
        o.insertadjacentelement("afterend",_table);
        o.style.display="none";
        _table.attachevent("onmouseover",function(){baction("over")});
        _table.attachevent("onmouseout",function(){baction("out")});
        _table.attachevent("onmousedown",function(){baction("down")});
        _table.attachevent("onmouseup",function(){baction("up")});
        _table.attachevent("onclick",function(){fnclick()});
        }
    }
function fnpropertychange(){
    var o=element;
    switch(event.propertyname.tostring().tolowercase()){
        case "checked":
            baction("up");
            break;
        }
    }
function fnclick(){
    var o=element;
    if(o.type=="checkbox"){
        if(o.disabled)return;
        if(o.checked){
            checkboximg.src=preb["checkbox_false"].src;
            }
        else{
            checkboximg.src=preb["checkbox_true"].src;
            }

        o.checked=!o.checked;
        }
    }
function baction(action){
    var o=element;
    if(o.type=="checkbox"){
        if(o.disabled)return;
        if(action=="up"||action=="over"){
            if(o.indeterminate){
                checkboximg.src=preb["checkbox_indeter_over"].src;
                }
            else if(o.checked){
                checkboximg.src=preb["checkbox_true_over"].src;
                }
            else{
                checkboximg.src=preb["checkbox_false_over"].src;
                }
            }
        if(action=="out"){
            if(o.indeterminate){
                checkboximg.src=preb["checkbox_indeter"].src;
                }
            else if(o.checked){
                checkboximg.src=preb["checkbox_true"].src;
                }
            else{
                checkboximg.src=preb["checkbox_false"].src;
                }

            }
        if(action=="down"){
            if(o.indeterminate){
                checkboximg.src=preb["checkbox_indeter_down"].src;
                }
            else if(o.checked){
                checkboximg.src=preb["checkbox_true_down"].src;
                }
            else{
                checkboximg.src=preb["checkbox_false_down"].src;
                }
            o.indeterminate=false;
            }
        }
    }
</script>
</public:component>

测试例子:
复制代码 代码如下:

<html>
<head>
<title> 新文档 </title>
<meta name="generator" content="editplus">
<meta name="author" content="flashsoft">
<meta name="keywords" content="">
<meta name="description" content="flashsoft">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style type="text/css">

input.checkbox{
    behavior:url("behaviors/checkbox.htc");cursor: hand;
    }

</style>
</head>
<body>
<input name="chk1" type="checkbox" class="checkbox" htc_mylabel="音乐" checked>
<input name="chk1" type="checkbox" class="checkbox" htc_mylabel="体育" disabled>
<input name="chk1" type="checkbox" class="checkbox" htc_mylabel="新闻">
<input name="chk1" type="checkbox" class="checkbox" htc_mylabel="不确定属性1" indeter="true" checked>
<input name="chk1" type="checkbox" class="checkbox" htc_mylabel="不确定属性2" indeter="true" disabled>
<input name="chk1" type="checkbox" class="checkbox" htc_mylabel="不确定属性3" indeter="true">
</body>
</html>

用htc实现CHECKBOX控件