Open an accordion while using <a href=“#link.html”>link</a> & <a name=“link”></a>

社会主义新天地 提交于 2021-01-29 05:21:56

问题


I usually get my codes from: https://www.w3schools.com. There is also an accordion to take with this code;

However when I use link -> to The accordion doesn't jump open but stays closed. Does someone know how to open the accordion if someone links a link which has content inside an accordion.

Thank you.

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].onclick = function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight){
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    }
  }
}
	
@charset "utf-8";
/* CSS Document */

 /* Style the buttons that are used to open and close the accordion panel */
button.accordion {
    background-color: #530010;
    color: #FFF;
	font-weight: bolder;
    cursor: pointer;
    padding: 10px;
    width: 100%;
    text-align: left;
    border: none;
    outline: none;
    transition: 0.4s;
	border-radius: 5px;
}

/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
button.accordion.active, button.accordion:hover {
    background-color: #a20121;
}

/* Style the accordion panel. Note: hidden by default */
div.panel {
    padding: 0px 5px;    
	margin-bottom: 5px;
	background-color: #none;
    max-height: 0px;
    overflow: hidden;
    transition: max-height 0.2s ease-out;
}

button.accordion:after {
    content: '\02795'; /* Unicode character for "plus" sign (+) */
    font-size: 13px;
    color: #FFF;
	font-weight: bolder;
    float: right;
    margin-left: 5px;
}

button.accordion.active:after {
    content: "\2796"; /* Unicode character for "minus" sign (-) */
}
<a href="schedule.html#0906">2018.09.06</a>(木) 今池GROW<br><br>

<button class="accordion"><a name="0906">■</a>2018/09/06(木)今池GROW </button>
<div class="panel">
<p>
Testing
</p>
</div>

回答1:


I had a little bit of trouble understanding what you were asking initially, but I took a stab at it anyways!

Added ID's to the .accordion buttons. Added .accordion-link class for your text links. When accordion-links are clicked, the hash is isolated, and used to trigger a click event on the accordion button with a corresponding ID.

Fiddle: https://jsfiddle.net/ya6vtosc/25/

JS

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].onclick = function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight){
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    }
  }
}

// get list of links by class
var links = document.getElementsByClassName("accordion-link");

var linksLength = links.length;
for(var i=0; i < linksLength; i++){
  links[i].onclick = function(e){
    // preventDefault is probably optional
    // depending on your application
    e.preventDefault();

    // isolate the hash
    var hash = e.path[0].hash;

    // remove # from hash
    hash = hash.substring(1, hash.length);

    // select by id using hash
    document.getElementById(hash).click();
  }

HTML

<a href="schedule.html#0906" class="accordion-link">2018.09.06</a>(木) 今池GROW<br><br>


<button class="accordion" id="0906">2018/09/06(木)今池GROW </button>
<div class="panel">
<p>
Testing
</p>
</div>

EDIT - I also removed your link tag from your button...not sure if a tags are allowed in buttons or not off the top of my head, but it didn't feel right so I kiboshed them.



来源:https://stackoverflow.com/questions/52006937/open-an-accordion-while-using-a-href-link-htmllink-a-a-name-link-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!