【问题标题】:Why doesn't my external jQuery click() event work in Jade?为什么我的外部 jQuery click() 事件在 Jade 中不起作用?
【发布时间】:2013-11-13 21:21:04
【问题描述】:

由于某种原因,我无法在单击任何元素时使此单击事件起作用,尽管当我单击正文时它确实起作用。我知道 ID 已正确分配,因为 CSS 也可以正常工作。这是JS:

$('h2').click(function() {
  alert( "h2 clicked." );
});//DOES NOT WORK

$('#whatever').click(function() {
  alert( "Whatever clicked." );
});//DOES NOT WORK

/*
$('body').click(function() {
  alert( "Body clicked." );
});
*/ //THIS WORKS WHEN UNCOMMENTED 

console.log('thank you!'); //THIS WORKS

这是一个玉文件:

extends ../layout

block content
  h2= post.title
  #whatever
   p= post.body
  span= post.created.toGMTString()
  p by #{post.author.fullname}
  br
  br
  div
    a(href="/post/edit/" + post.id) Edit
    | |
    a(href="/post/remove/" + post.id) Delete
  .comments
    ul
      each comment in comments
        li
          p.text= comment.text
          p.author= comment.author
    if session.isLoggedIn
      form(method='post', action="/post/comment/" + post.id)
        textarea(name='text')
        input(type='submit', value='Save')

这是另一个涉及的玉文件:

html5
html
  head
    title= pageTitle
    link(rel='stylesheet', type='text/css', href='/stylesheets/styles.css')
    //script(type='text/javascript', src='/js/jquery-1.10.2.min.js')
    script(type='text/javascript', src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js')
    script(type='text/javascript', src='/js/scriptures.js')

 body
    header
      h1= pageTitle
    a(href="/") Home
    section.login
      ul
        if session.isLoggedIn
          li
            a(href="/logout") Logout
        else
          li
            a(href="/login") Login
          li
            a(href="/signup") Create Account
    section.content
      block content

【问题讨论】:

  • 答案是stackoverflow.com/questions/3953611/… 谁能告诉我为什么我需要document.ready()?
  • 因为 document ready 是一种分离 html 和 js 加载时间的方法。当文档准备好时,所有 dom 元素都会被加载,因此您可以安全地绑定事件处理程序。这就是我解释它的方式,但我不是深入知识的专家。如果在未加载该 dom 元素时执行绑定,那么您的绑定根本不起作用。

标签: jquery click pug


【解决方案1】:

$(document).ready(function(){ $('#whatever').click(function() { alert("点击了什么。"); }); });

Am I going crazy? jQuery .click() doesn't seem to work

【讨论】:

    猜你喜欢
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多