Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 锚定标签不能正常工作_Javascript_Html_Jquery_Css_Ejs - Fatal编程技术网

Javascript 锚定标签不能正常工作

Javascript 锚定标签不能正常工作,javascript,html,jquery,css,ejs,Javascript,Html,Jquery,Css,Ejs,当我点击p时,什么都没有发生,但当我点击div的任何其他部分时,它工作正常。我不知道为什么会发生这种情况。请帮助我 <%- include('./partials/header');-%> <%- include('./partials/flash');-%> <% posts.forEach(function(post){ %> <a href="/post/<%= post._id %>" id="posta

当我点击p时,什么都没有发生,但当我点击div的任何其他部分时,它工作正常。我不知道为什么会发生这种情况。请帮助我

<%- include('./partials/header');-%>
<%- include('./partials/flash');-%>
<% posts.forEach(function(post){ %>
<a href="/post/<%= post._id %>" id="posta">
<div id="post">
<h3 class="text-dark" id="title"><%- post.Title %></h3>
<p class="text-secondary" id="date"><small><%=post.Author.name %><br>
<%=post._id.getTimestamp().toLocaleDateString()%></small></p>
<p class="h6 text-dark" id="Postcontent"><%- post.Content.substring(0,200)+"..." %><span class="h6 text-success">Read more</span>
</p>
</div>
</a>
<span id="border"></span>
<% }) %>
<%- include('./partials/footer');-%>
更新: 这很奇怪,但我观察到,当我点击第一个项目的p时,它不起作用,但对于所有其他项目,它都起作用

链接至网站:

基于您的代码。问题在于您编写js代码的方式。问题不在问题中提到的地方(感谢链接)。您犯的错误是新手犯的错误之一(从不在循环中放入静态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();
});

正如@rajesh paudel所提到的,有一个小错误,但很重要

 Never user static ids in loops. 
此代码违反标准,因此不正确。它将无法通过验证检查,而且应该如此

ID必须是唯一的。阅读这里:为了 为了


对我来说很好。周围是否有其他代码干扰?您的关闭
标签(用于
阅读更多内容)
未关闭properly@abney317.Thank谢谢你指出错误。尽管纠正了错误,我也遇到了同样的问题。@Kerri很抱歉回复得太晚。我将发布完整的代码。请也粘贴以html呈现的输出,这样我们可以帮助您感谢您!
 Never user static ids in loops. 
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.