【发布时间】:2014-07-13 00:59:12
【问题描述】:
我在 jQuery 中有一个类,我想获取已单击元素的类。 (元素在同一个类中)。
<!DOCTYPE html>
<html>
<head>
<title>Try jQuery 2.0.0 Online</title>
<script src="http://code.jquery.com/jquery-2.0.0.min.js"></script>
<script>
$(document).ready(function() {
$('.shikhar').click(function(){
var decide=$(this).attr('class');
//here I want to know what was the class of the element that was clicked
alert(decide);
if(decide=='a'){
$(this).find('.b').addClass("selected");
$(this).find('.a').addClass("highlight");
}else{
$(this).find('.a').addClass("selected");
$(this).find('.b').addClass("highlight");
}
});
});
</script>
<style>
.selected {
color:red;
}
.highlight {
background:yellow;
}
</style>
</head>
<body>
<div class="shikhar">
<div class="a">Superman</div>
<div class="b">Hulk</div>
</div>
<div class="shikhar">
<div class="a">Spiderman</div>
<div class="b">Batman</div>
</div>
</body>
</html>
这是一个小提琴 - http://jsfiddle.net/6QjN6/
【问题讨论】:
-
你的标题说你想要类名,但你的问题说你想要 id...它是哪一个?
-
如果要获取ID,必须先将ID放入元素中。他们没有身份证。
标签: javascript jquery html css