在这里编码新手。我一直在尝试使用css,javascript和html在手风琴格式中创建链接。但是,链接适用于除Firefox之外的所有浏览器。我做错了什么?`
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
&#13;
button.accordion {
background-color: #222222;
color: #ffffff;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}
button.accordion.active, button.accordion:hover {
background-color: #666666;
}
div.panel {
padding: 0 18px;
display: none;
background-color: #FFFFFF;
}
div.panel.show {
display: block;
}
&#13;
<button class="accordion">1. Item 1     <a href="http://google.com">link 1</a>    </button>
<div class="panel">
<p>
Line 1<br \>
Line 2 <br \> <br \>
</p>
</div>
<button class="accordion">1. Item 2     <a href="http://google.com">link 1</a>    </button>
<div class="panel">
<p>
Line 1<br \>
Line 2 <br \> <br \>
</p>
</div>
&#13;
我已将代码保存在JSFIDDLE中。
答案 0 :(得分:0)
尝试使用JQuery手风琴而不是基本的Javascript。
这是一个适用于不同浏览器的例子:
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Accordion</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(function() {
$("#accordion").accordion({
alwaysOpen: false,
active: false,
autoheight: false,
clearStyle: true
}).find('a').click(function(ev) {
window.location.href = $(this).attr("href");
});
});
</script>
<style>
.ui-accordion2-header {
position: absolute;
right: 10px;
top: 10px;
width: 345px;
}
.ui-accordion2-header a {
width: auto;
display: inline;
}
</style>
</head>
<body>
<div id="accordion" class="ui-accordion2-group">
<h3 data-sectionid="1">
Section 1 <a href="https://www.google.com">link 1</a>
</h3>
<div>
<p>Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.</p>
</div>
<h3 data-sectionid="2">
Section 2 <a href="http://www.youtube.com">link 2</a>
</h3>
<div>
<p>Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.</p>
</div>
</div>
</body>
</html>