简单CoffeeScript和jQuery事件的奇怪问题



该事件在通配符选择器上激发良好,但如果我为选择器使用特定的ID,则会失败。

这项工作:

$("*").click ->
  console.log "entered event."
  $("#tellafriend").dialog
    modal: true
    buttons:
      Ok: ->
        $(this).dialog "close"

然而,事实并非如此:

$("#tell-a-friend").click ->
  console.log "entered event."
  $("#tellafriend").dialog
    modal: true
    buttons:
      Ok: ->
        $(this).dialog "close"

以及我的相关HTML:

<ul class="actions">
    <li><a href="#">Home</a></li>
    <li>|</li>
    <li><a href="#" id="tell-a-friend">Tell a Friend</a></li>
</ul>

我是不是在CoffeeScript中遗漏了一些东西,使其发挥作用?也许jQuery选择器在CoffeeScript中的格式不同?

这是我的应用程序提供的渲染Javascript(Rails在提供页面时将CoffeeScript转换为JS):

(function() {
  $("#tell-a-friend").click(function() {
    console.log("works");
    return $("#tellafriend").dialog({
      modal: true,
      buttons: {
        Ok: function() {
          return $(this).dialog("close");
        }
      }
    });
  });
}).call(this);

是否有可能在DOM完全加载之前执行代码。尝试将其包装在文档就绪处理程序中,以便在DOM完全加载后应用任何单击处理程序。实际上,它可能只在body元素上执行,因为这是执行脚本时唯一可用的元素。

最新更新