日期:2014-05-17  浏览次数:21107 次

css中可否定义onclick事件?
如题


------解决方案--------------------
ie下可以,俺曾经试验过,不过这么做确实有点儿bt,哈

还是用js实现更好一些!

JS实现,L@_@K
JScript code

<!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>
    <title> new document </title>
    <meta name="generator" content="editplus" />
    <meta name="author" content="Gao YiXiang" />
    <meta name="email" content="yixianggao@126.com" />
    <meta name="keywords" content="javascript dhtml dom" />
    <meta name="description" content="I love web development." />
</head>
<body>
    <input type="text" id="" class="enableClick" />
    <input type="checkbox" id="" class="enableClick" />
    <textarea id="" rows="10" cols="10" class="enableClick"></textarea><br />
    <input type="text" id="" />
    <input type="checkbox" id="" />
    <textarea id="" rows="10" cols="10"></textarea>

    <script type="text/javascript">
    <!--
function setElementsByClassName(className)
{
    var allEles = document.body.childNodes;

    for (var i=0; i<allEles.length; i++)
    {
        if (allEles[i].className 
            && allEles[i].className == className)
        {
            allEles[i].onclick = function()
            {
                alert(this.tagName + " " + this.type);
            };
        }
    }
}
setElementsByClassName("enableClick");
    //-->
    </script>
</body>
</html>

------解决方案--------------------
css+expression实现,不推荐!
注:IE6sp1下可用,其他浏览器未测试!
L@_@K
HTML code

<!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>
    <title> new document </title>
    <meta name="generator" content="editplus" />
    <meta name="author" content="Gao YiXiang" />
    <meta name="email" content="yixianggao@126.com" />
    <meta name="keywords" content="javascript dhtml dom" />
    <meta name="description" content="I love web development." />
    <style type="text/css">
.enableClick
{
    btAttribute: expression(onclick=function(){alert(this.tagName + " " + this.type);});
}
    </style>
</head>
<body>
    <input type="text" id="" class="enableClick" />
    <input type="checkbox" id="" class="enableClick" />
    <textarea id="" rows="10" cols="10" class="enableClick"></textarea><br />
    <input type="text" id="" />
    <input type="checkbox" id="" />
    <textarea id="" rows="10" cols="10"></textarea>
</body>
</html>