简体   繁体   English

Onclick显示/隐藏-Javascript

[英]Onclick show/hide - Javascript

I'm working a very simple program that show the solution of the problem when the user click on the solution link. 我正在工作一个非常简单的程序,当用户单击解决方案链接时,它会显示问题的解决方案。 I'm trying to do this without using jQuery. 我正在尝试不使用jQuery来执行此操作。

So the first thing that I did was to add a click to my p . 因此,我要做的第一件事是向我的p添加一个点击。 But now I'm lost and I don't know how to display the solution. 但是现在我迷路了,我不知道如何显示解决方案。

Any ideas on how to fix this problem? 关于如何解决此问题的任何想法?

 var solutionLink = document.querySelectorAll("p.solutionLink"); function openSolution(){ document.getElementsByClassName('.solution').nextSibling.style.display = "block"; } for(var i=0; i< solutionLink.length; i++){ var solutions = solutionLink[i]; solutions.addEventListener('click', openSolution, false); } 
 .solutions {display:none;} 
 <p>This problem #1</p> <p class="solutionLink">Click here to see solution</p> <div class="solutions"> This is the solution to problem #1 </div> <p>This problem #2</p> <p class="solutionLink">Click here to see solution</p> <div class="solutions"> This is the solution to problem #2 </div> 

Your first problem is your css selector: The class solutions is really called solution . 您的第一个问题是CSS选择器:类solutions实际上称为solution

Next I would recommend making use of the data-attributes . 接下来,我建议您使用data-attributes This way you can change your DOM-structure and don't have to change any js. 这样,您可以更改DOM结构,而不必更改任何js。 Alternatively I would at least wrap every solution in its own container. 或者,我至少将每个解决方案包装在自己的容器中。

Below is a (in my opinion) nice implementation, but I still want to outline some problems with your js: 下面是一个(我认为)不错的实现,但是我仍然想概述一下您的js的一些问题:

  • getElementsByClassName('.solution')̀ should be getElementsByClassName('solution')̀ , since the . 自以来, getElementsByClassName('.solution')̀ should be getElementsByClassName('solution') . is not part of the class name 不是班级名称的一部分
  • getElementsByClassName returns elementS, as in more than one. getElementsByClassName返回elementS,如多个元素一样。 You will have to access them using [0] 您将必须使用[0]访问它们

 var solutionLink = document.querySelectorAll("p.solutionLink"); function openSolution(){ document.getElementById(this.dataset.target).style.display = "block"; } for(var i=0; i< solutionLink.length; i++){ var solutions = solutionLink[i]; solutions.addEventListener('click', openSolution, false); } 
 .solution {display:none;} 
 <p>This problem #1</p> <p class="solutionLink" data-target="sol1">Click here to see solution</p> <div id="sol1" class="solution"> This is the solution to problem #1 </div> <p>This problem #2</p> <p class="solutionLink" data-target="sol2">Click here to see solution</p> <div id="sol2" class="solution"> This is the solution to problem #2 </div> 

 var solutionLinks = document.querySelectorAll(".solutionLink"); for (var i = 0; i < solutionLinks.length; i++) { solutionLinks[i].addEventListener("click", function() { this.nextElementSibling.classList.toggle("hidden"); }); } 
 .hidden { display:none; } 
 <p>This problem #1</p> <p class="solutionLink">Click here to see solution</p> <div class="hidden"> This is the solution to problem #1 </div> <p>This problem #2</p> <p class="solutionLink">Click here to see solution</p> <div class="hidden"> This is the solution to problem #2 </div> 

Why are you currently attempting to retrieve the nextSibling of the very element that you wish to display (the textNode ) ? 您当前为什么要尝试检索要显示的元素( textNode )的nextSibling Using the event Object target (the p.solutionLink ), access the adjacent elementNode ( div.solution ) . 使用event Object targetp.solutionLink ),访问相邻的elementNodediv.solution )。 According to your code the follow solution should work for you. 根据您的代码,以下解决方案应为您工作。

function openSolution(event){
  event.target.nextElementSibling.style.display = "block";
}

PS why is your CSS selector plural .solutions ? PS为什么您的CSS选择器为.solutions复数?

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

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