Aspen EDR 二次开发教程(19):LLM-Agent 与 EDR 数据面协作——工具边界与防幻觉闸门
2026/9/28 3:11:33
在网页开发中,实现长文本的展开/收起功能通常可以通过以下几种方法来完成:
利用CSS的text-overflow属性和max-height属性,结合过渡效果,可以实现简单的展开/收起效果。
<style>.text-container{max-height:100px;/* 初始高度 */overflow:hidden;transition:max-height 0.5s ease;}.text-container.expanded{max-height:1000px;/* 展开后的高度,根据实际内容调整 */}.toggle-btn{cursor:pointer;color:blue;text-decoration:underline;margin-top:5px;display:inline-block;}</style><divclass="text-container"id="textContainer">这里是长文本内容...</div><spanclass="toggle-btn"onclick="toggleText()">展开/收起</span><script>functiontoggleText(){constcontainer=document.getElementById('textContainer');container.classList.toggle('expanded');}</script>使用JavaScript或jQuery可以更灵活地控制展开/收起效果,包括动画、状态切换等。
<style>.text-container{max-height:100px;overflow:hidden;transition:max-height 0.5s ease;}.text-container.expanded{max-height:none;/* 或者设置为一个足够大的值 */}.toggle-btn{cursor:pointer;color:blue;text-decoration:underline;margin-top:5px;display:inline-block;}</style><divclass="text-container"id="textContainer">这里是长文本内容...</div><spanclass="toggle-btn"onclick="toggleText()">展开/收起</span><script>functiontoggleText(){constcontainer=document.getElementById('textContainer');constbtn=document.querySelector('.toggle-btn');if(container.classList.contains('expanded')){container.classList.remove('expanded');btn.textContent='展开';}else{container.classList.add('expanded');btn.textContent='收起';}}</script><style>.text-container{max-height:100px;overflow:hidden;transition:max-height 0.5s ease;}.text-container.expanded{max-height:none;}.toggle-btn{cursor:pointer;color:blue;text-decoration:underline;margin-top:5px;display:inline-block;}</style><divclass="text-container"id="textContainer">这里是长文本内容...</div><spanclass="toggle-btn"id="toggleBtn">展开/收起</span><scriptsrc="https://code.jquery.com/jquery-3.6.0.min.js"></script><script>$(document).ready(function(){$('#toggleBtn').click(function(){$('#textContainer').toggleClass('expanded');$(this).text($('#textContainer').hasClass('expanded')?'收起':'展开');});});</script>如果你使用的是React、Vue等前端框架,或者Bootstrap等UI库,它们通常提供了现成的组件或插件来实现展开/收起功能。
import React, { useState } from 'react'; function TextExpandCollapse() { const [isExpanded, setIsExpanded] = useState(false); return ( <div> <div style={{ maxHeight: isExpanded ? 'none' : '100px', overflow: 'hidden', transition: 'max-height 0.5s ease' }}> 这里是长文本内容... </div> <button onClick={() => setIsExpanded(!isExpanded)}> {isExpanded ? '收起' : '展开'} </button> </div> ); } export default TextExpandCollapse;<template> <div> <div :style="{ maxHeight: isExpanded ? 'none' : '100px', overflow: 'hidden', transition: 'max-height 0.5s ease' }"> 这里是长文本内容... </div> <button @click="isExpanded = !isExpanded"> {{ isExpanded ? '收起' : '展开' }} </button> </div> </template> <script> export default { data() { return { isExpanded: false }; } }; </script>