Javascript 获取已单击的元素的类名
我在jQuery中有一个类,我想获取已单击元素的类。(元素在同一类中)Javascript 获取已单击的元素的类名,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我在jQuery中有一个类,我想获取已单击元素的类。(元素在同一类中) 在线尝试jQuery 2.0.0 $(文档).ready(函数(){ $('.shikhar')。单击(函数(){ var decise=$(this.attr('class'); //这里我想知道被点击的元素的类是什么 警惕(决定); 如果(决定=='a'){ $(this.find('.b').addClass(“selected”); $(this.find('.a').addClass(“highlight”);
在线尝试jQuery 2.0.0
$(文档).ready(函数(){
$('.shikhar')。单击(函数(){
var decise=$(this.attr('class');
//这里我想知道被点击的元素的类是什么
警惕(决定);
如果(决定=='a'){
$(this.find('.b').addClass(“selected”);
$(this.find('.a').addClass(“highlight”);
}否则{
$(this.find('.a').addClass(“选定”);
$(this.find('.b').addClass(“highlight”);
}
});
});
.选定{
颜色:红色;
}
.突出显示{
背景:黄色;
}
超人
残骸
蜘蛛侠
蝙蝠侠
这里有一个小提琴-解决方案1 使用
e.target
获取事件开始的元素
解决方案2 您还可以使用类似于
.shikhar>div
的选择器将侦听器放置在您希望单击的特定元素上,然后这将是您所期望的。看起来是这样的:
请注意,如果使用此选择器,则必须更改addClass()
,才能找到同级:
if(decide=='a'){
$(this).parent().find('.b').addClass("selected");
$(this).parent().find('.a').addClass("highlight");
}else{
$(this).parent().find('.a').addClass("selected");
$(this).parent().find('.b').addClass("highlight");
}
或
建议
如果用户可以多次单击,您可能希望删除“选定”和“突出显示”类,这样它们就不会同时使用这两个类
另外,检查类是否有“a”或“b”,而不是唯一的类。这将确保即使添加了其他类也能正常工作。以下是更新版本:
您需要将单击处理程序更改为:
$('.shikhar div').click(function(){});
这是一个更新的标题,你的标题说你想要类名,但你的问题说你想要id。。。它是哪一个?如果你想得到ID,你必须先把ID放在元素中。他们没有ID。这不会添加类,因为它无法在.shikhar div
中找到.a
或.b
。谢谢你的回答。斯梅尼:)我试过了,但是如果(decise='a'){$(This),这段代码就不起作用了。查找('.b').addClass(“selected”);$(This)。查找('.a').addClass(“highlight”);}。否则{$(this.find('.a').addClass(“选中”);$(this.find('.b').addClass(“突出显示”);}@Shikhar_Bansal,在我的帖子中有两个不同的答案。看起来你使用了第二个答案,但没有按照我提到的那样更新addclass部分。如果你看看JSFiddles,它们都能像我所期望的那样工作。@Shikhar_Bansal,我在我的建议中解决了代码的一些其他问题。有了这些,你可以采取行动了一旦应用了选择/突出显示,就可以更改它们。有几个问题没有解决,其中一个是它造成的。
if(decide=='a'){
$(this).parent().find('.b').addClass("selected");
$(this).parent().find('.a').addClass("highlight");
}else{
$(this).parent().find('.a').addClass("selected");
$(this).parent().find('.b').addClass("highlight");
}
if(decide=='a'){
$(this).next('.b').addClass("selected");
$(this).addClass("highlight");
}else{
$(this).prev('.a').addClass("selected");
$(this).addClass("highlight");
}
$(document).ready(function () {
$('.shikhar > div').click(function (e) {
$this = $(this);
$parent = $this.parent(); // just to prevent so much jquery object creation
var decide;
if ($this.hasClass('a')) {
decide = 'a';
} else if ($this.hasClass('b')) {
decide = 'b';
}
console.log(decide);
if (decide == 'a') {
$parent.find('.b').removeClass("highlight").addClass("selected");
$parent.find('.a').removeClass("selected").addClass("highlight");
} else {
$parent.find('.a').removeClass("highlight").addClass("selected");
$parent.find('.b').removeClass("selected").addClass("highlight");
}
});
});
$('.shikhar div').click(function(){});
$('.shikhar div').click(function(e){
e.preventDefault();
var decide = this.className;
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");
}
});