Javascript 将类添加到所选div(div不是唯一的)
我有不同的Javascript 将类添加到所选div(div不是唯一的),javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有不同的div,它们有一个id=“item”。当我选择一个div时,应将一个新类添加到已选择div的a。当ID不唯一时,有没有办法做到这一点 HTML: 编辑:我正在使用delegate函数,因为我使用PHP创建了所有div,否则单击事件不会注册。没有非唯一ID,这是无效的。除了无效ID之外,这不起作用,因为您使用的是针对所有链接而不是特定链接的$(“a”) HTML: 另请注意,假设您使用的是最新版本的jQuery,我将delegate更改为on,如果没有,请返回delegate不要使用非唯
div
,它们有一个id=“item”
。当我选择一个div
时,应将一个新类添加到已选择div的a
。当ID不唯一时,有没有办法做到这一点
HTML:
编辑:我正在使用
delegate
函数,因为我使用PHP创建了所有div,否则单击事件不会注册。没有非唯一ID,这是无效的。除了无效ID之外,这不起作用,因为您使用的是针对所有链接而不是特定链接的$(“a”)
HTML:
另请注意,假设您使用的是最新版本的jQuery,我将
delegate
更改为on
,如果没有,请返回delegate
不要使用非唯一ID,这是无效的。除了无效ID之外,这不起作用,因为您使用的是针对所有链接而不是特定链接的$(“a”)
HTML:
另请注意,假设您使用的是最新版本的jQuery,我将delegate
更改为on
,如果没有,请返回delegate
$(this)。find(“a”).addClass(“active”) $(this.find(“a”).addClass(“active”) 您应该尝试以下方法:
<div class="something" id="item1">
<a href="#" class="otherthing">...</a>
</div>
<div class="something" id="item2">
<a href="#" class="otherthing">...</a>
</div>
<div class="something" id="item3">
<a href="#" class="otherthing">...</a>
</div>
<div class="something" id="item4">
<a href="#" class="otherthing">...</a>
</div>
$(".something").on('click', '.otherthing', function() {
$(this).addClass("active");
});
$(“.something”).on('click','.otherthing',function(){
$(此).addClass(“活动”);
});
您应该尝试以下方法:
<div class="something" id="item1">
<a href="#" class="otherthing">...</a>
</div>
<div class="something" id="item2">
<a href="#" class="otherthing">...</a>
</div>
<div class="something" id="item3">
<a href="#" class="otherthing">...</a>
</div>
<div class="something" id="item4">
<a href="#" class="otherthing">...</a>
</div>
$(".something").on('click', '.otherthing', function() {
$(this).addClass("active");
});
$(“.something”).on('click','.otherthing',function(){
$(此).addClass(“活动”);
});
id必须是唯一的。jQuery在找到第一个匹配元素后停止搜索。id
是唯一的属性。不应该有两个元素具有相同的ID。这应该可以正常工作,只要用“a”替换“a”
,此ID必须是唯一的。jQuery在找到第一个匹配元素后停止搜索。id
是唯一的属性。不应该有两个元素具有相同的ID。这应该可以正常工作,只要您将“a”
替换为“a”,这就是我一直在寻找的,也是迄今为止唯一有效的解决方案。谢谢你!为什么我的解决方案不起作用?这段代码也不会处理无效的ID,也不会处理单击多个div将在所有div上保持活动的事实。这正是我一直在寻找的,也是迄今为止唯一有效的解决方案。谢谢你!为什么我的解决方案不起作用?此代码也不会处理无效ID,也不会处理单击多个div将在所有div上保持活动的事实。
<div class="something" id="item1">
<a href="#" class="something">...</a>
</div>
<div class="something" id="item2">
<a href="#" class="something">...</a>
</div>
<div class="something" id="item3">
<a href="#" class="something">...</a>
</div>
<div class="something" id="item4">
<a href="#" class="something">...</a>
</div>
// I doubt your actual site uses something in both cases,
// but I made this more explicit.
$("body").on( 'div.something', 'click', function() {
$('.active').removeClass( 'active' );
// Use this to add active to div
$( this ).addClass( "active" );
// Use this to add active to link
$( this ).find( 'a' ).addClass('active");
});
<div class="something" id="item1">
<a href="#" class="otherthing">...</a>
</div>
<div class="something" id="item2">
<a href="#" class="otherthing">...</a>
</div>
<div class="something" id="item3">
<a href="#" class="otherthing">...</a>
</div>
<div class="something" id="item4">
<a href="#" class="otherthing">...</a>
</div>
$(".something").on('click', '.otherthing', function() {
$(this).addClass("active");
});