javascript - JQuery 帮助在 div 中显示更多/更少格式

标签 javascript jquery html css

我在查找和创建解决方案时遇到了一个问题,该解决方案使用 JQuery 在目标 div 中设置格式来显示更多/更少的功能。在下面的代码片段中,当我填写页面的表单时,空元素会填充文本。在 150 个文本字符可见(忽略格式和标签)时,显示更多/更少功能需要启动并隐藏大于 150 个字符的链接以显示更多或更少。

我试过使用这个 http://code-tricks.com/jquery-read-more-less-example/作为基础,但我无法让该代码与其中带有标签的 div 一起使用。

<div class="search-keywords more">
    <p style="display: none;" id="searchCriteria">All records</p>
    <p style="display: inline;" id="emEnvSubclass">EM Environmental Subclass = </p>
    <p style="display: inline; font-style: italic;" id="emEnvSubclassValue">Atomsphere OR Created Greenspace OR Forests</p>
    <p style="display: inline;" id="emSourceColl"> AND EM Source/Collection = </p>
    <p style="display: inline; font-style: italic;" id="emSourceCollValue">Other OR US EPA</p>
    <p style="display: none;" id="cicesSec">null</p>
    <p style="display: none; font-style: italic;" id="cicesSecValue">null</p>
    <p style="display: none;" id="fegsEnvSubclass">null</p>
    <p style="display: none; font-style: italic;" id="fegsEnvSubclassValue">null</p>
</div>

预先感谢您的帮助!

最佳答案

更新的答案:

如果不是一定要固定在150个字符,可以根据div大小,可以用这个插件:dotdotdot.frebsite.nl

原始答案: 这是一个工作 fiddle :http://jsfiddle.net/manishie/mgz8tec0/

你需要替换:

var content = $(this).html();

与:

var content = $(this).text();

当您计算字符数时,这将去除 html 标签。

关于javascript - JQuery 帮助在 div 中显示更多/更少格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25879112/

相关文章:

javascript - 使用 JS 考虑旋转调整多个对象的大小

javascript - JS/HTML ButtonText.InnerHTML 没有响应

javascript - 为什么多次调用 setTimeout() 会造成如此大的延迟?

jquery - 更新所有div元素的内部内容

javascript - JQuery 获取点击事件的 .on 监听器的子选择器的值

html - 样式化内联元素的分隔符

javascript - 简单的 javascript if 和 else。如果 url 返回,否则滚动到 anchor

javascript - 如何使用angularjs在复选框列表中推送选定的值

javascript - 如何使用 Knockout JS 添加类?

javascript - CSV 数据和 JS/HTML