【发布时间】:2021-08-17 18:11:49
【问题描述】:
我尝试应用此post 将阅读更多/阅读更少的 JS 集成到 django 模板中。我想限制我的帖子应用程序。 200 个字符,并为用户提供阅读更多内容的选项。
这个脚本似乎工作正常......在我的情况下不是那么多。 它显示“阅读更多”选项,但一旦单击它就不会触发任何事件。也就是说,它不会打开整个文本。它会显示一个彩色测试“阅读更多”,但会注意到其他情况。
这是post中提供的答案:
{% for post in posts %}
{% if post.content|length > 150 %}
<p class="half-content" id="half-{{post.id}}">{{ post.content|truncatechars:150 }}<a data-id="{{post.id}}" href="javascript:void();" class="show-hide-btn">read more</a></p>
<p class="full-content" id="full-{{post.id}}" style="display: none;">{{ post.content }}</p></div>
{% else %}
<p>{{ post.content }}</p>
{% endif %}
{% endfor %}
<script>
$(document).ready(function(){
$(".show-hide-btn").click(function(){
var id = $(this).data("id");
$("#half-"+id).hide();
非常感谢您的任何意见。
【问题讨论】:
标签: javascript django django-templates