简体   繁体   English

如何只允许一个特定类的元素

[英]How to allow only one element with a specific class

I have a few div's that onclick fire a function. 我有一些div可以onclick触发功能。 I want to make it so that when clicked the div that was just clicked has the class .selected . 我想这样做,以便在单击时刚单击的div具有.selected类。 I then want it to be that when you click on another one of the elements, the .selected class is removed from the other element and then applied to the new one. 然后,我希望它是当您单击另一个元素时,将从另一个元素中删除.selected类,然后将其应用于新元素。 When I click the first one and then click one that comes later it works fine. 当我单击第一个,然后单击后面的一个时,它工作正常。 It is when I click the last one and then one that came before it that it just stays. 当我单击最后一个,然后单击它之前的那个时,它才停留。 What am I doing wrong? 我究竟做错了什么?

 function choosediv(element){ if(document.querySelectorAll('.selected').length != 0){ document.getElementsByClassName('c')[0].classList.remove('selected'); } element.classList.add('selected'); } 
 .c{ background-color: pink; cursor: pointer; } .selected{ background-color: red; } 
 <div class="c" onclick="choosediv(this)">Click me</div> <div class="c" onclick="choosediv(this)">Click me</div> <div class="c" onclick="choosediv(this)">Click me</div> 

The problem is that you are only ever removing the selected class from the 0th element matched in this line: 问题是您只能从此行中匹配的第0个元素中删除selected类:

document.getElementsByClassName('c')[0].classList.remove('selected');

By selecting the 0th element of the array returned by getElementsByClassName('c')[0] , every chosen div not at index 0 will remain selected. 通过选择由getElementsByClassName('c')[0]返回的数组的第0个元素,每个位于索引0的选定div将保持选中状态。

You should perhaps make use of a forEach iteration over the elements selected by getElementsByClassName('c') , to remove the selected class from each one, rather than just the 0th one. 您也许应该在getElementsByClassName('c')选择的元素上使用forEach迭代,以从每个类中删除selected类,而不仅仅是从第0个类中删除。

You can just remove the selected class from the .c that is .selected (using querySelector , which returns the first element that matches the query, if it exists, hence the if ), and then add selected class to the element that was clicked: 您只需从.selected.c中删除selected类(使用querySelector ,它返回与查询匹配的第一个元素(如果存在),然后返回if ),然后将selected类添加到单击的element中:

 .c { background-color: pink; cursor: pointer; } .selected { background-color: red; } 
 <div class="c" onclick="choosediv(this)">Click me</div> <div class="c" onclick="choosediv(this)">Click me</div> <div class="c" onclick="choosediv(this)">Click me</div> <script> function choosediv(element) { var curr_sel = document.querySelector('.c.selected'); if (curr_sel) curr_sel.classList.remove('selected'); element.classList.add('selected'); } </script> 

document.getElementsByClassName('c') - this will select all element that has class c . document.getElementsByClassName('c') -这将选择所有具有类c元素。

you are removing only first element that has class c by selecting collection's element at index 0 要删除具有类只有第一个元素c通过索引0选择集合的元素

document.getElementsByClassName('c')[0]

You should iterate over your collection and remove class one by one. 您应该遍历集合并逐一删除类。

function choosediv(element){
    if (document.querySelectorAll('.selected').length > 0) {
        document.querySelectorAll('.selected').forEach(el => {
          el.classList.remove('selected');
        })
    }

    element.classList.add('selected');
}

That's an easy one-liner. 这是一个简单的方法。 Just iterate over the collection of c's. 只需遍历c的集合即可。 When the element equals the one you're currently iterating, add the class, otherwise remove it: element等于您当前正在迭代的element ,添加类,否则将其删除:

 const cs = document.querySelectorAll('.c'); function choosediv(element) { for (const c of cs) { c.classList[c === element ? 'add' : 'remove']('selected'); } } 
 .c { background-color: pink; cursor: pointer; } .selected { background-color: red; } 
 <div class="c" onclick="choosediv(this)">Click me</div> <div class="c" onclick="choosediv(this)">Click me</div> <div class="c" onclick="choosediv(this)">Click me</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM