我需要制作一个页面,在那里你可以看到评论的开头,但必须点击'点击更多'查看评论的其余部分

I need to make a page where you can see the beginning of a review, but have to click on 'click more' to see the rest of the review

本文关键字:评论 余部 在那里 开头 一个      更新时间:2023-09-26

我正在为一家企业建立一个网站,他们想要一个专门介绍他们产品好评的页面。

然而,他们不希望巨大的段落占据他们的页面。有没有一种方法可以让每个评论都有他们可以点击的东西,比如只点击其中的一行,然后"点击这里",可以单独展开每个评论?

我刚才请了一位绅士帮我,但只进了一次门。我对所有3个都使用了相同的javascript,我认为这是不正确的。

此外,我认为这将根据评论的长度来扩大页面的高度。我之所以提到这个,是因为我在底部有一个页脚。

无论如何,我想是JavaScript吗?

<div class="comment">
This is the first line of the comment 
<span id="extended_comment"   style="display:none;">and  this    is the rest of the    comment     that is hiddden.</span> <a href="#" id="toggle">click here for more</a></div>
<script type="text/javascript">
var toggle = document.getElementById('toggle');
toggle.onclick = function() {
var extended = document.getElementById('extended_commen…
if(this.innerHTML == 'click here for more') {
 extended.style.display = 'inline';
 this.innerHTML = 'click here for less';
 } else {
 extended.style.display = 'none';
this.innerHTML = 'click here for more';
}
};
</script>

将代码封装在一个函数中,并将必要的id传递给该函数,以便设置每个元素的onclick属性。

function setExpandOnclick(toggleButton, extendedComment) {
  // your code here, using the arguments rather than hard-coded string ids
}
setExpandOnclick("toggle-button1", "expanded-comment1");
setExpandOnclick("toggle-button2", "expanded-comment2");

你走在了正确的轨道上。