我想在我的项目中使用以下代码,我要隐藏和显示6个对象列表,是否需要定义6个ID框?(我想用Hide
来单独控制这6个ID和Show
函数)以及演示“一些文本。一些文本。一些文本”中框的内容。都在for loop
中,我可以显示该for loop
的完整文本字段吗?因为文本太长,它们会从屏幕上消失。
此外,我可以将Hide
设置为默认值,以下代码将显示for loop
的内容,无论我进行了什么更改。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box p {
width: 360px;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.box p:hover {
overflow: visible;
}
</style>
</head>
<body>
<button id="statusChange">Show</button>
<div class="box" id="box">
<p>1、Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. </p>
<p>2、Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts.</p>
<p>3、Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts.</p>
<p>4、Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts. Some texts.</p>
<p>5、......</p>
</div>
<script>
document.getElementById('statusChange').onclick = function() {
var text = this.innerText
if (text == 'Hide') {
document.getElementById('box').style.cssText = 'display:none'
this.innerText = 'Show'
} else if (text == 'Show') {
document.getElementById('box').style.cssText = 'display:block'
this.innerText = 'Hide'
}
}
</script>
</body>
</html>