Я делаю галерею продуктов.Существует название продукта (h3), а затем параграф с описанием.У меня есть промежуток в середине, чтобы скрыть часть текста, если не выбран текст «ЧИТАТЬ БОЛЬШЕ».Проблема в том, что функция запускается для каждого продукта, а не только для одного, на который нажали.Если я выберу второй продукт, первый также будет переключен.
Вот HTML:
<h3>
PLS-CADD
</h3>
<p>The standard edition of PLS-CADD is a line design program that includes all the terrain, sag-tension, loads, clearances and drafting functions necessary for the design of an entire power
<span id="dots">...</span>
<span id="more">line. Also includes PLS-CADD/LITE and PLS-CADD/ULTRALITE, but not any of the other items listed below (compare editions).</span>
</p>
<a onclick="myFunction()" id="myBtn">Read more</a>
Вот CSS:
#more {display: none;}
Вотфункция:
<script>
function myFunction() {
var dots = document.getElementById("dots");
var moreText = document.getElementById("more");
var btnText = document.getElementById("myBtn");
if (dots.style.display === "none") {
dots.style.display = "inline";
btnText.innerHTML = "Read more";
moreText.style.display = "none";
} else {
dots.style.display = "none";
btnText.innerHTML = "Read less";
moreText.style.display = "inline";
}
}
</script>
РЕДАКТИРОВАТЬ: я реструктурировал каждый продукт, чтобы выглядеть следующим образом -
<h3>
PLS-CADD
</h3>
<p>The standard edition of PLS-CADD is a line design program that includes all the terrain, sag-tension, loads, clearances and drafting functions necessary for the design of an entire power <span id="more1">line. Also includes PLS-CADD/LITE and PLS-CADD/ULTRALITE, but not any of the other items listed below (compare editions).</span></p>
<a onClick="showhide('more1')" id="myBtn">Read more</a>
Скрытый текст каждого последующего продукта будет иметь идентификаторы "more2, more3 и т. д. ...
Моя обновленная функция:
<script type="text/javascript" >
function showhide(toggleID){
var toggleDiv = document.getElementById(toggleID);
if(toggleDiv.style.display = "none"){
toggleDiv.style.display = 'block';
}else {
toggleDiv.style.display = 'none';
}
}
</script>
Каждое описание правильно расширяется, но я не могу их закрыть.