使用Colapsable列表隐藏和显示图像

时间:2017-01-06 02:00:42

标签: javascript html

我有一个带有单选按钮的可折叠列表。根据单击的单选按钮,弹出不同的图像。这就是我所拥有的 -



$('#radio1-1').click(function (e) {
    e.stopPropagation();
    $('#img1').show();
    $('#img2').hide();
    $('#img3').hide();
    $('#img4').hide();
});
$('#radio1-2').click(function (e) {
    e.stopPropagation();
    $('#img1').hide();
    $('#img2').show();
    $('#img3').hide();
    $('#img4').hide();
});
$('#radio2-1').click(function (e) {
    e.stopPropagation();
    $('#img1').hide();
    $('#img2').hide();
    $('#img3').show();
    $('#img4').hide();
});
$('#radio2-2').click(function (e) {
    e.stopPropagation();
    $('#img1').hide();
    $('#img2').hide();
    $('#img3').hide();
    $('#img4').show();
});

$('body').click(function () {
    $('#img1,#img2,#img3,#img4').hide();
});


var chanceoflive5 = 1;
var user;
var chanceoflive6 = 1;
var user2;
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
    acc[i].onclick = function(){
        if(!this.classList.contains("active")) {
            closeAccordions();
        }
        this.classList.toggle("active");
        this.nextElementSibling.classList.toggle("show");
  }
}

function closeAccordions() {
    for (i = 0; i < acc.length; i++) {
        acc[i].classList.remove("active");
        acc[i].nextElementSibling.classList.remove("show");

    }
}

function choose(choice){
    user = choice;
}

function changechanceoflive2(){
    if (user == 'bolts') {
        chanceoflive5 = 1;
    }
    else if (user == 'washers') {
        chanceoflive5 = 2;
    }

}

function choose2(choice2){
    user2 = choice2;
}

function changechanceoflive3(){
    if (user2 == 'tie') {
        chanceoflive6 = 1;
    }
    else if (user2 == 'struts') {
        chanceoflive6 = 2;
    }

}

function nextpage(){
  var chanceoflive7 = chanceoflive5 + chanceoflive6;
  alert(chanceoflive7);
}
&#13;
button.accordion {
    background-color: #eee;
    color: #444;
    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: #ddd;
}

button.accordion:after {
    content: '\002B';
    color: #777;
    font-weight: bold;
    float: right;
    margin-left: 5px;
}

button.accordion.active:after {
    content: "\2212";
}

div.panel {
    padding: 0 18px;
    max-height: 0;
    overflow: hidden;
    transition: 0.6s ease-in-out;
    opacity: 0;
}

div.panel.show {
    opacity: 1;
    max-height: 500px;
}

body {
	font-family: sans-serif;
	font-weight: normal;
	margin: 10px;
	color: #999;
  background-image: url("bridge.jpg");
}

form {
	margin: 40px 0;
}

div {
	clear: both;
	margin: 0 50px;
}

label {
  border-radius: 3px;
  border: 1px solid #D1D3D4
}

/* hide input */
input.radio:empty {
	margin-left: -999px;
}

/* style label */
input.radio:empty ~ label {
	position: relative;
	float: left;
	line-height: 2.5em;
	text-indent: 3.25em;
	margin-top: 2em;
	cursor: pointer;
	-webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
}

input.radio:empty ~ label:before {
	position: absolute;
	display: block;
	top: 0;
	bottom: 0;
	left: 0;
	content: '';
	width: 2.5em;
	background: #D1D3D4;
	border-radius: 3px 0 0 3px;
}

/* toggle hover */
input.radio:hover:not(:checked) ~ label:before {
	content:'\2714';
	text-indent: .9em;
	color: #C2C2C2;
}

input.radio:hover:not(:checked) ~ label {
	color: #888;
}

/* toggle on */
input.radio:checked ~ label:before {
	content:'\2714';
	text-indent: .9em;
	color: #9CE2AE;
	background-color: #4DCB6D;
}

input.radio:checked ~ label {
	color: #777;
}
button.accordion {
    background-color: #eee;
    color: #444;
    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: #ddd;
}

button.accordion:after {
    content: '\002B';
    color: #777;
    font-weight: bold;
    float: right;
    margin-left: 5px;
}

button.accordion.active:after {
    content: "\2212";
}

div.panel {
    padding: 0 18px;
    max-height: 0;
    overflow: hidden;
    transition: 0.6s ease-in-out;
    opacity: 0;
}

div.panel.show {
    opacity: 1;
    max-height: 500px;
}

body {
	font-family: sans-serif;
	font-weight: normal;
	margin: 10px;
	color: #999;
}

form {
	margin: 40px 0;
}

div {
	clear: both;
	margin: 0 50px;
}

label {
  border-radius: 3px;
  border: 1px solid #D1D3D4
}

/* hide input */
input.radio:empty {
	margin-left: -999px;
}

/* style label */
input.radio:empty ~ label {
	position: relative;
	float: left;
	line-height: 2.5em;
	text-indent: 3.25em;
	margin-top: 2em;
	cursor: pointer;
	-webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
}

input.radio:empty ~ label:before {
	position: absolute;
	display: block;
	top: 0;
	bottom: 0;
	left: 0;
	content: '';
	width: 2.5em;
	background: #D1D3D4;
	border-radius: 3px 0 0 3px;
}

/* toggle hover */
input.radio:hover:not(:checked) ~ label:before {
	content:'\2714';
	text-indent: .9em;
	color: #C2C2C2;
}

input.radio:hover:not(:checked) ~ label {
	color: #888;
}

/* toggle on */
input.radio:checked ~ label:before {
	content:'\2714';
	text-indent: .9em;
	color: #9CE2AE;
	background-color: #4DCB6D;
}

input.radio:checked ~ label {
	color: #777;
}

#container {
  width: 500px;
  margin: auto;
}

/*Neon*/
p {
  text-align: center;
  font-size: 7em;
  margin: 20px 0 20px 0;
}

a {
  text-decoration: none;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  transition: all 0.5s;
}

p:nth-child(2) a {
  font-size: 0.5em;
  color: #228DFF;
  font-family: Iceland;
}

p:nth-child(2) a:hover {
  -webkit-animation: neon2 1.5s ease-in-out infinite alternate;
  -moz-animation: neon2 1.5s ease-in-out infinite alternate;
  animation: neon2 1.5s ease-in-out infinite alternate;
}

p a:hover {
  color: #ffffff;
}
/*glow for webkit*/

@-webkit-keyframes neon2 {
  from {
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #228DFF, 0 0 70px #228DFF, 0 0 80px #228DFF, 0 0 100px #228DFF, 0 0 150px #228DFF;
  }
  to {
    text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 20px #228DFF, 0 0 35px #228DFF, 0 0 40px #228DFF, 0 0 50px #228DFF, 0 0 75px #228DFF;
  }
}

@-moz-keyframes neon2 {
  from {
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #228DFF, 0 0 70px #228DFF, 0 0 80px #228DFF, 0 0 100px #228DFF, 0 0 150px #228DFF;
  }
  to {
    text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 20px #228DFF, 0 0 35px #228DFF, 0 0 40px #228DFF, 0 0 50px #228DFF, 0 0 75px #228DFF;
  }
}

@keyframes neon2 {
  from {
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #228DFF, 0 0 70px #228DFF, 0 0 80px #228DFF, 0 0 100px #228DFF, 0 0 150px #228DFF;
  }
  to {
    text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 20px #228DFF, 0 0 35px #228DFF, 0 0 40px #228DFF, 0 0 50px #228DFF, 0 0 75px #228DFF;
  }
}

/*REEEEEEEEEEESPONSIVE*/

@media (max-width: 650px) {
  #container {
    width: 100%;
  }
  p {
    font-size: 3.5em;
  }
}

img {
    display:none;
}
body {
    border:1px solid #999;
}
&#13;
<hr>

<button class="accordion">Foundation Bolting</button>
<div class="panel">

    <div>
      <input type="radio" name="radio" id="radio1-1" class="radio" onclick="choose('bolts')" checked/>
      <label for="radio1-1">Foundation Bolts</label>
    </div>

     <div>
      <input type="radio" name="radio" id="radio1-2" class="radio" onclick="choose('washers')">
      <label for="radio1-2">Plate Washers</label>
    </div>


</div>

<hr>

<button class="accordion">Wall Bracing</button>
<div class="panel">

	 <div>
      <input type="radio" name="radio2" id="radio2-1" class="radio" onclick="choose2('tie')" checked/>
      <label for="radio2-1">Strong Tie Retrofit Connectors</label>
    </div>

     <div>
      <input type="radio" name="radio2" id="radio2-2" class="radio" onclick="choose2('struts')">
      <label for="radio2-2">Angled Iron Struts</label>
    </div>

</div>

<hr>

<img src="foundationbolts.jpg" id='img1'>
<img src="platewasher.jpg" id='img2'>
<img src="strongtie.jpg" id='img3'>
<img src="ironstruts.png" id='img4'>

<div id="container">

  <p></p>

  <p><a href="finalresult.html" onClick="changechanceoflive2(); changechanceoflive3(); nextpage()" align=center>
    Submit
  </a></p>

</div>
&#13;
&#13;
&#13;

虽然,可折叠列表不会打开,如果我删除了使图像显示为消失的js代码,它就会打开。如何使图像按照我的要求工作?

1 个答案:

答案 0 :(得分:1)

尝试以下代码(也添加jQuery引用):

将jQuery内容包装在$(document).ready(function() { ... });中,因为这样可以确保在进行任何jQuery调用之前加载jQuery。

&#13;
&#13;
$(document).ready(function() {
$('#radio1-1').click(function (e) {
    //e.stopPropagation();
    $('#img1').show();
    $('#img2').hide();
    $('#img3').hide();
    $('#img4').hide();
});
$('#radio1-2').click(function (e) {
    //e.stopPropagation();
    $('#img1').hide();
    $('#img2').show();
    $('#img3').hide();
    $('#img4').hide();
});
$('#radio2-1').click(function (e) {
    //e.stopPropagation();
    $('#img1').hide();
    $('#img2').hide();
    $('#img3').show();
    $('#img4').hide();
});
$('#radio2-2').click(function (e) {
    //e.stopPropagation();
    $('#img1').hide();
    $('#img2').hide();
    $('#img3').hide();
    $('#img4').show();
});

$('body').click(function () {
    $('#img1,#img2,#img3,#img4').hide();
});

});
&#13;
button.accordion {
    background-color: #eee;
    color: #444;
    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: #ddd;
}

button.accordion:after {
    content: '\002B';
    color: #777;
    font-weight: bold;
    float: right;
    margin-left: 5px;
}

button.accordion.active:after {
    content: "\2212";
}

div.panel {
    padding: 0 18px;
    max-height: 0;
    overflow: hidden;
    transition: 0.6s ease-in-out;
    opacity: 0;
}

div.panel.show {
    opacity: 1;
    max-height: 500px;
}

body {
	font-family: sans-serif;
	font-weight: normal;
	margin: 10px;
	color: #999;
  background-image: url(https://placehold.it/60x60);
}

form {
	margin: 40px 0;
}

div {
	clear: both;
	margin: 0 50px;
}

label {
  border-radius: 3px;
  border: 1px solid #D1D3D4
}

/* hide input */
input.radio:empty {
	margin-left: -999px;
}

/* style label */
input.radio:empty ~ label {
	position: relative;
	float: left;
	line-height: 2.5em;
	text-indent: 3.25em;
	margin-top: 2em;
	cursor: pointer;
	-webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
}

input.radio:empty ~ label:before {
	position: absolute;
	display: block;
	top: 0;
	bottom: 0;
	left: 0;
	content: '';
	width: 2.5em;
	background: #D1D3D4;
	border-radius: 3px 0 0 3px;
}

/* toggle hover */
input.radio:hover:not(:checked) ~ label:before {
	content:'\2714';
	text-indent: .9em;
	color: #C2C2C2;
}

input.radio:hover:not(:checked) ~ label {
	color: #888;
}

/* toggle on */
input.radio:checked ~ label:before {
	content:'\2714';
	text-indent: .9em;
	color: #9CE2AE;
	background-color: #4DCB6D;
}

input.radio:checked ~ label {
	color: #777;
}
button.accordion {
    background-color: #eee;
    color: #444;
    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: #ddd;
}

button.accordion:after {
    content: '\002B';
    color: #777;
    font-weight: bold;
    float: right;
    margin-left: 5px;
}

button.accordion.active:after {
    content: "\2212";
}

div.panel {
    padding: 0 18px;
    max-height: 0;
    overflow: hidden;
    transition: 0.6s ease-in-out;
    opacity: 0;
}

div.panel.show {
    opacity: 1;
    max-height: 500px;
}

body {
	font-family: sans-serif;
	font-weight: normal;
	margin: 10px;
	color: #999;
}

form {
	margin: 40px 0;
}

div {
	clear: both;
	margin: 0 50px;
}

label {
  border-radius: 3px;
  border: 1px solid #D1D3D4
}

/* hide input */
input.radio:empty {
	margin-left: -999px;
}

/* style label */
input.radio:empty ~ label {
	position: relative;
	float: left;
	line-height: 2.5em;
	text-indent: 3.25em;
	margin-top: 2em;
	cursor: pointer;
	-webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
}

input.radio:empty ~ label:before {
	position: absolute;
	display: block;
	top: 0;
	bottom: 0;
	left: 0;
	content: '';
	width: 2.5em;
	background: #D1D3D4;
	border-radius: 3px 0 0 3px;
}

/* toggle hover */
input.radio:hover:not(:checked) ~ label:before {
	content:'\2714';
	text-indent: .9em;
	color: #C2C2C2;
}

input.radio:hover:not(:checked) ~ label {
	color: #888;
}

/* toggle on */
input.radio:checked ~ label:before {
	content:'\2714';
	text-indent: .9em;
	color: #9CE2AE;
	background-color: #4DCB6D;
}

input.radio:checked ~ label {
	color: #777;
}

#container {
  width: 500px;
  margin: auto;
}

/*Neon*/
p {
  text-align: center;
  font-size: 7em;
  margin: 20px 0 20px 0;
}

a {
  text-decoration: none;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  transition: all 0.5s;
}

p:nth-child(2) a {
  font-size: 0.5em;
  color: #228DFF;
  font-family: Iceland;
}

p:nth-child(2) a:hover {
  -webkit-animation: neon2 1.5s ease-in-out infinite alternate;
  -moz-animation: neon2 1.5s ease-in-out infinite alternate;
  animation: neon2 1.5s ease-in-out infinite alternate;
}

p a:hover {
  color: #ffffff;
}
/*glow for webkit*/

@-webkit-keyframes neon2 {
  from {
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #228DFF, 0 0 70px #228DFF, 0 0 80px #228DFF, 0 0 100px #228DFF, 0 0 150px #228DFF;
  }
  to {
    text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 20px #228DFF, 0 0 35px #228DFF, 0 0 40px #228DFF, 0 0 50px #228DFF, 0 0 75px #228DFF;
  }
}

@-moz-keyframes neon2 {
  from {
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #228DFF, 0 0 70px #228DFF, 0 0 80px #228DFF, 0 0 100px #228DFF, 0 0 150px #228DFF;
  }
  to {
    text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 20px #228DFF, 0 0 35px #228DFF, 0 0 40px #228DFF, 0 0 50px #228DFF, 0 0 75px #228DFF;
  }
}

@keyframes neon2 {
  from {
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #228DFF, 0 0 70px #228DFF, 0 0 80px #228DFF, 0 0 100px #228DFF, 0 0 150px #228DFF;
  }
  to {
    text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 20px #228DFF, 0 0 35px #228DFF, 0 0 40px #228DFF, 0 0 50px #228DFF, 0 0 75px #228DFF;
  }
}

/*REEEEEEEEEEESPONSIVE*/

@media (max-width: 650px) {
  #container {
    width: 100%;
  }
  p {
    font-size: 3.5em;
  }
}

img {
    display:none;
}
body {
    border:1px solid #999;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<hr>

<button class="accordion">Foundation Bolting</button>
<div class="panel">

    <div>
      <input type="radio" name="radio" id="radio1-1" class="radio" onclick="choose('bolts')" checked/>
      <label for="radio1-1">Foundation Bolts</label>
    </div>

     <div>
      <input type="radio" name="radio" id="radio1-2" class="radio" onclick="choose('washers')">
      <label for="radio1-2">Plate Washers</label>
    </div>


</div>

<hr>

<button class="accordion">Wall Bracing</button>
<div class="panel">

	 <div>
      <input type="radio" name="radio2" id="radio2-1" class="radio" onclick="choose2('tie')" checked/>
      <label for="radio2-1">Strong Tie Retrofit Connectors</label>
    </div>

     <div>
      <input type="radio" name="radio2" id="radio2-2" class="radio" onclick="choose2('struts')">
      <label for="radio2-2">Angled Iron Struts</label>
    </div>

</div>

<hr>

<img src="https://placehold.it/70x70" id='img1'>
<img src="https://placehold.it/80x80" id='img2'>
<img src="https://placehold.it/90x90" id='img3'>
<img src="https://placehold.it/100x100" id='img4'>

<div id="container">

  <p></p>

  <p><a href="finalresult.html" onClick="changechanceoflive2(); changechanceoflive3(); nextpage()" align=center>
    Submit
  </a></p>

</div>

<script>
var chanceoflive5 = 1;
var user = '';
var chanceoflive6 = 1;
var user2 = '';
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
    acc[i].onclick = function(){
        if(!this.classList.contains("active")) {
            closeAccordions();
        }
        this.classList.toggle("active");
        this.nextElementSibling.classList.toggle("show");
  }
}

function closeAccordions() {
    for (i = 0; i < acc.length; i++) {
        acc[i].classList.remove("active");
        acc[i].nextElementSibling.classList.remove("show");

    }
}

function choose(choice){
    user = choice;
}

function changechanceoflive2(){
    if (user == 'bolts') {
        chanceoflive5 = 1;
    }
    else if (user == 'washers') {
        chanceoflive5 = 2;
    }

}

function choose2(choice2){
    user2 = choice2;
}

function changechanceoflive3(){
    if (user2 == 'tie') {
        chanceoflive6 = 1;
    }
    else if (user2 == 'struts') {
        chanceoflive6 = 2;
    }

}

function nextpage(){
  var chanceoflive7 = chanceoflive5 + chanceoflive6;
  alert(chanceoflive7);
}
</script>
&#13;
&#13;
&#13;