简体   繁体   English

选择具有所有其他ID都不是'this.id'的元素

[英]Select elements with all other ID that is not 'this.id'

I have 4 elements and within onclick() only one of the block div will show; 我有4个元素,在onclick()只有一个div block会显示;

HTML: HTML:

<!--elements that will toggle block div to show-->
<p onclick="expand(this.id)" id="p1"></p>
<p onclick="expand(this.id)" id="p2"></p>
<p onclick="expand(this.id)" id="p3"></p>
<p onclick="expand(this.id)" id="p4"></p>
<!--block div-->
<div id="block_p1"></div>
<div id="block_p2"></div>
<div id="block_p3"></div>
<div id="block_p4"></div>

JS: JS:

function expand(e) {
document.getElementById("block_" + e).style.display = "block";
document.getElementById(e).style.backgroundColor = "#425a94";
}

The problem is when I click the second element after the first, says I click p2 after p1 , the block div-- block_p1 doesn't disappear as block_p2 is shown, how do I hide the first block after the second is clicked? 问题是,当我单击第一个元素之后的第二个元素时,说我单击了p1之后的p2 ,块div-- block_p1不会随着显示的block_p2消失, 如何在单击第二个元素之后隐藏第一个块? If I didn't use the parameter I'd do something like this: 如果不使用该参数,则将执行以下操作:

function expand() {
document.getElementById("block_p2").style.display = "block";
document.getElementById("p2").style.backgroundColor = "#425a94";
document.getElementById("block_p1").style.display = "none";
}

I don't know how to do the same in the case of the one with a parameter. 我不知道如何在带有参数的情况下执行相同的操作。 Also in the case that third element is selected I need to hide the first two blocks as well. 同样在选择第三个元素的情况下,我也需要隐藏前两个块。

You first need to hide all divs that start with id expanded_ , just add this line before rest of your code. 首先 ,您需要隐藏与ID开始的所有div expanded_ ,只是你的代码的其余部分之前加入这一行。

var allExpanded = document.querySelectorAll( "div[id^='expanded_']" );
Array.from( allExpanded ).forEach( s => (s.style.display = "none") );

Your functions becomes 您的功能变为

function expand(e) 
{
    //first hide all
    var allExpanded = document.querySelectorAll( "div[id^='expanded_']" );
    Array.from( allExpanded ).forEach( s => (s.style.display = "none") );
    //then show specific
    document.getElementById("expanded_" + e).style.display = "block";

    document.getElementById(e).style.backgroundColor = "#425a94";
    document.getElementById("toolbar_expand").style.display = "block";
}

From my previous answer , a small amendment to pick up all display elements which can be looped over in the function to remove the class that was previously added: 从我先前的答案中 ,进行了一个小的修改,以选择所有可以在函数中循环显示的显示元素,以删除先前添加的类:

 const buttons = document.querySelectorAll('.button'); const slides = document.querySelectorAll('.slide'); buttons.forEach(button => { button.addEventListener('click', handleClick, false); }); function handleClick(e) { const id = e.target.dataset.id; slides.forEach(slide => slide.classList.remove('show')); const slide = document.querySelector(`.slide[data-id="${id}"]`); slide.classList.add('show'); } 
 .slide { display: none; } .show { display: block; } 
 <p class="button" data-id="1">icon1</p> <p class="button" data-id="2">icon2</p> <p class="button" data-id="3">icon3</p> <div class="slide" data-id="1">blocki1</div> <div class="slide" data-id="2">blocki2</div> <div class="slide" data-id="3">blocki3</div> 

You can use the id match selector to get all the div with id having block_ as substring and then hide all of them except the clicked one. 您可以使用id匹配选择器来获取idblock_作为子字符串的所有div ,然后隐藏除单击的所有div所有div

 function expand(id) { document.getElementById('block_'+id).style.display = "block"; document.getElementById(id).style.backgroundColor = "#425a94"; document.querySelectorAll('[id^="block_"]').forEach(function(elem){ if(elem.id !== 'block_'+id){ elem.style.display = "none"; var pId = elem.id.split('_')[1]; document.getElementById(pId).style.backgroundColor = "#fff"; } }); } 
 div{ display: none; } 
 <!--elements that will toggle block div to show--> <p onclick="expand(this.id)" id="p1">p1 click</p> <p onclick="expand(this.id)" id="p2">p2 click</p> <p onclick="expand(this.id)" id="p3">p3 click</p> <p onclick="expand(this.id)" id="p4">p4 click</p> <!--block div--> <div id="block_p1">P1</div> <div id="block_p2">P2</div> <div id="block_p3">P3</div> <div id="block_p4">P4</div> 

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

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