我有以下代码:
<script type="text/javascript">
$("*").click(function(event){
var x = event.target;
if (x.nodeName == "DIV"){
alert(x.attr("class"));
}
})
</script>
这会抛出一个“undefined”异常... 有没有其他方法可以获得触发“click”事件的元素的类?提前感谢!
event.target
,您可以直接引用this
。if (this.nodeName == "DIV") alert( $(this).attr("class"));
可以翻译为:“请注意,您不必参考event.target
,而是可以直接参考this
。if (this.nodeName == "DIV") alert( $(this).attr("class"));
”。如果this
的节点名称是DIV
,则弹出它的类名。 - h2ooooooo