所以,我收到一条错误消息:
未捕获的TypeError:无法读取null的属性“样式”
我确实意识到还有很多其他答案,但是所有这些答案中,最简单的答案就是等待页面加载。单击按钮(并且整个页面已加载)后,我的问题发生了。
您可能想知道的一些事情
JS
function ReadMore() {
console.log('Initiated function ReadMore')
var dots = document.getElementById("dots");
var moreText = document.getElementById("more");
var btnText = document.getElementById("readmore");
if (dots.style.display === Null) { //ERROR ON THIS LINE
dots.style.display = "inline";
btnText.innerHTML = "Read more";
moreText.style.display = "none";
} else {
dots.style.display = "none";
btnText.innerHTML = "Read less";
moreText.style.display = "inline";
}
console.log('Executed and finished function ReadMore')
}
Django HTML
{% for img in products %}
{{ img|safe }}
{% if forloop.counter == 25 %}
<div class="products"><div class="desc"><button onclick="ReadMore()" id="readmore">Read more</button></div></div><span id="more">
{% endif %}
{% if forloop.last and forloop.counter >= 25 %}
</span>
{% endif %}
{% endfor %}