原生表格文本过长展示问题,参考layui长文本,点击出现文本域

需要引入layui文件

效果

html代码, 记得class="layui-table"; 这要有

复制代码
    <form class="js-ajax-form" method="post" autocomplete="off">
      

        <table class="layui-table">
            <thead>
            <tr>
                <th>退烧药</th>
            </tr>
            </thead>
            <tbody>
            <foreach name="list" item="vo">
                <tr>
                    
                    <td>
                        <if condition="!empty($vo.drug_names)">
                            <if condition="mb_strlen($vo.drug_names, 'utf-8') gt 10">
                                <span class="layui-inline age-text"
                                      onclick="toggleAgeTextarea(this, '{$vo.drug_names}')">
                                    {$vo.drug_names|mb_substr=0,10}...
                                </span>
                                <textarea class="layui-textarea age-textarea"
                                          style="display: none; width: 200px; height: 40px;"
                                          readonly
                                          onblur="toggleAgeTextarea(this)"></textarea>
                                <else/>
                                <span>{$vo.drug_names}</span>
                            </if>
                        </if>
                    </td>
                     
                </tr>
            </foreach>
            </tbody>
        </table>
    </form>

js 可以弄公共部分每个页面都引入一下

复制代码
    <!-- 显示隐藏长文本框 -->
    <script>
        // 切换显示文本与文本框
        window.toggleAgeTextarea = function (element, currentValue) {
            var textarea = element.nextElementSibling;

            if (textarea.style.display === 'none') {
                // 显示文本框
                element.style.display = 'none';
                textarea.value = currentValue;
                textarea.style.display = 'block';

                // 定位文本框在点击位置
                var rect = element.getBoundingClientRect();
                textarea.style.position = 'absolute';
                textarea.style.left = rect.left + 'px';
                textarea.style.top = rect.top + 'px';
                textarea.style.zIndex = '1000';
                textarea.style.resize = 'both';
                textarea.style.overflow = 'auto';
                textarea.style.boxShadow = '0 2px 6px rgba(0,0,0,0.1)';
                textarea.style.minHeight = '70px';
                textarea.style.minWidth = '200px';

                textarea.focus();
            } else {
                // 隐藏文本框,显示文本
                var updatedValue = textarea.value;
                textarea.style.display = 'none';
                element.innerText = updatedValue.length > 10 ? updatedValue.substr(0, 10) + "..." : updatedValue;
                element.style.display = 'inline';
            }
        };

        // 监听文本框失去焦点
        document.addEventListener('blur', function (event) {
            var target = event.target;
            if (target.classList.contains('age-textarea')) {
                var textarea = target;
                var spanElement = textarea.previousElementSibling;

                // 隐藏文本框并更新显示文本
                textarea.style.display = 'none';
                var updatedValue = textarea.value;
                spanElement.innerText = updatedValue.length > 10 ? updatedValue.substr(0, 10) + "..." : updatedValue;
                spanElement.style.display = 'inline';
            }
        }, true);
    </script>
相关推荐
忠实米线16 小时前
使用lottie.js播放json动画文件
开发语言·javascript·json
0思必得016 小时前
[Web自动化] Selenium浏览器对象方法(操纵浏览器)
前端·python·selenium·自动化·web自动化
Marshmallowc16 小时前
React页面刷新数据丢失怎么办?彻底掌握LocalStorage持久化与状态回填的最佳实践
前端·javascript·react.js
郝学胜-神的一滴16 小时前
Vue国际化(i18n)完全指南:原理、实践与最佳方案
前端·javascript·vue.js·程序人生·前端框架
tkevinjd16 小时前
2-初识JS
开发语言·前端·javascript·ecmascript·dom
梦65016 小时前
React 类组件与函数式组件
前端·javascript·react.js
Coder_Boy_17 小时前
基于SpringAI的在线考试系统-成绩管理功能实现方案
开发语言·前端·javascript·人工智能·spring boot
幻云201017 小时前
Python深度学习:从筑基与巅峰
前端·javascript·vue.js·人工智能·python
Light6017 小时前
庖丁解牛:深入JavaScript内存管理,从内存泄漏到AI赋能的性能优化
javascript·人工智能·性能优化·内存管理·垃圾回收·内存泄漏·v8引擎
_OP_CHEN17 小时前
【前端开发之HTML】(三)HTML 常见标签(下):图文、链接与实战,解锁网页交互新姿势!
前端·html·交互·前端开发·网页开发·界面美化