Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将类添加到所选div(div不是唯一的)_Javascript_Jquery_Html_Css - Fatal编程技术网

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");
});