【发布时间】:2020-11-23 19:30:19
【问题描述】:
大家好,我正在创建一个社交媒体消息应用程序,现在我正在了解创建一个按钮来关注人们的基础知识。
当您单击按钮时,它应该将按钮从“关注”更改为“取消关注”。那么追随者的计数器应该增加 1。
到目前为止,这是我的代码,它没有做任何事情。我没有收到任何错误,但它也没有做任何事情。
有人能弄清楚我做错了什么吗?谢谢
network.js:
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('followButton').addEventListener('click', () => follow_user());
});
function follow_user() {
const element = document.getElementById('followButton');
element.value = "Un-Follow";
const element2 = document.getElementById('followers');
element2.textContent += 1;
}
profile.html:
{% extends "network/layout.html" %}
{% block body %}
<h2>{{user.username}}'s Profile</h2>
<p id="followers">Followers: {{user.followers}}</p>
<p>Follows: {{user.follows}}</p>
{% for newquery in newqueries reversed %}
<div class="newpost"><p>{{ newquery.body }}</p><p>By: {{ newquery.username }} on: {{ newquery.timestamp }} Likes: {{ newquery.likes }}</p></div>
{% endfor %}
<input type="button" value="Follow" id="followButton">
{% endblock %}
【问题讨论】:
-
我不确定,但我很快用眼睛扫描了你的代码,我可以在这里看到问题: const element2 = document.getElementById('followers');您尝试使用 id 'followers' 访问元素,但是当您查看 HTML 标记时,没有具有此类 id 的元素,我看到您使用 class="followers" 代替,因此请尝试更改它。
-
我更新了代码,如您所见,它仍然没有做任何事情。
标签: javascript dom event-handling