2 回答

TA贡献1982条经验 获得超2个赞
根据您的代码。问题在于您编写 js 代码的方式。问题出在问题中提到的其他地方(感谢链接)。您犯的错误是菜鸟错误之一(永远不要将 static id 放在循环中)。id是一样的。基于 js 的工作方式,它会寻找唯一的 id,但重复的类可能没问题。所以你当前的代码是
document.getElementById("Postcontent").addEventListener("click", function(event){
event.preventDefault();
});
将Postcontent从 id更改为 class 并添加以下代码
var postcontent = document.querySelectorAll(".Postcontent");
postcontent.addEventListener('click', function(event){
event.preventDefault();
});

TA贡献1946条经验 获得超4个赞
@rajesh-paudel 提到了一个小错误,但很重要。
Never user static ids in loops.
此代码违反了标准,因此是不正确的。它会失败验证检查,它应该。
ID 必须是唯一的。阅读此处:HTML 5 的HTML 4.x 规范
When specified on HTML elements, the id attribute value must be
unique amongst all the IDs in the element's tree and must contain at
least one character. The value must not contain any ASCII whitespace.
添加回答
举报