事件在通配符选择器上触发很好,但如果我为选择器使用特定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);
答案 0 :(得分:2)
是否可能在DOM完全加载之前执行代码。尝试将其包装在文档就绪处理程序中,以便在DOM完全加载后应用任何单击处理程序。实际上,它可能只在body元素上执行,因为这是脚本执行时唯一可用的元素。