Expanding paragraph (html) for news section

旧巷老猫 提交于 2019-12-08 06:45:41

问题


I'm not sure how to go about this. I have a news section on a website I maintain and I'm trying to get it to display about 10 news items. Then I want to give users the option to click "more" for the rest, and the "less" for back to default. The document is xhtml and css and I'm trying to use as little javascript as possible. Any ideas?


回答1:


If you want all the news article to actually be on the page, just invisible, versus having to request more from the server when the user clicks more, and you don't want to use the awesome javascript library called JQuery, then I suggest you get familiar with the Javascript method, document.getElementById. You can use it like this:

<a id="moreless" href="javascript:moretoggle()">More</a>
<div>some news</div>
<div id="morediv" style="display:none;">more news</div>

<script>
function moretoggle() {
    document.getElementById("morediv").style.display = document.getElementById("morediv").style.display == "none" ? "block" : "none";
    document.getElementById("moreless").innerHTML = document.getElementById("moreless").innerHTML == "More" ? "Less" : "More";
}
</script>


来源:https://stackoverflow.com/questions/1752891/expanding-paragraph-html-for-news-section

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