Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
当我想显示超过 600 个字符的文本时,我正在使用“truncatechars”。
我正在做的是:当文本有超过 600 个字符时,我会截断字符以仅显示一个小文本,而不是我有一个“显示更多”按钮来显示所有文本。
{{ad.description|truncatechars:"600"}} {% if ad.description|length > 150 %}<a href="#">ver mais...</a> {% endif %}
如何动态删除“truncatechars”属性?
您不必使用truncatechars. 您可以执行此功能的纯 javascript/jquery 实现。
truncatechars
您可以使用一些插件。
或者
如果您要自己实现它,请查看此链接
您所要做的就是添加一个包装器{{ad.description|safe}}
{{ad.description|safe}}
如果你想“显示更多”,那么你根本不应该使用 django 的模板标签,因为它们是在服务器端呈现的。这意味着客户端(浏览器)将永远不会得到超过前 600 个字符的任何内容。而是用javascript来做。有很多插件和教程可以处理这个问题(例如这里、这里和这里)