Javascript jQuery.on(”单击“)-替代.live()

Javascript jQuery.on(”单击“)-替代.live(),javascript,html,jquery,jquery-on,Javascript,Html,Jquery,Jquery On,在我的项目中,我在div中添加了几个按钮: $(".tab-content").append("<div class='landing_content'><button class='off-screen-nav-button btn btn-info btn_edit' data-effeckt='effeckt-off-screen-nav-right-overlay'>Edit</button></div>"); 为什么这不起作用?我习惯使

在我的项目中,我在div中添加了几个按钮:

$(".tab-content").append("<div class='landing_content'><button class='off-screen-nav-button btn btn-info btn_edit' data-effeckt='effeckt-off-screen-nav-right-overlay'>Edit</button></div>");
为什么这不起作用?我习惯使用.live(),但它已被弃用


谢谢。

在您的标记中,您有
btn\u edit
,在jQuery中,您有
btn edit

更改标记
class
以匹配jQuery选择器,如下所示:

$(".tab-content").append("<div class='landing_content'><button class='off-screen-nav-button btn btn-info btn-edit' data-effeckt='effeckt-off-screen-nav-right-overlay'>Edit</button></div>");
$(“.tab内容”)。追加(“编辑”);

在标记中有
btn\u edit
,在jQuery中有
btn edit

更改标记
class
以匹配jQuery选择器,如下所示:

$(".tab-content").append("<div class='landing_content'><button class='off-screen-nav-button btn btn-info btn-edit' data-effeckt='effeckt-off-screen-nav-right-overlay'>Edit</button></div>");
$(“.tab内容”)。追加(“编辑”);

您的方法不起作用的原因(除了关于
btn edit
所指出的以外)可能是因为您试图将事件绑定到DOM中实际不存在的元素,直到添加该元素之后,该元素以前一直处于活动状态,但已被折旧,但您可以这样做:

//bind the event to the document which always exists and specify the selector
//as the second argument 
$(document).on("click",".tab-content .btn-edit",function(){
    console.log('edit');
});

希望这有助于查看文档中的jquerys以获得更多帮助。

您的方法不起作用的原因(除了关于
btn edit
指出的以外)可能是因为您试图将事件绑定到一个元素,而该元素在添加之后才在DOM中实际存在,过去,LIVE已经贬值,但您可以这样做:

//bind the event to the document which always exists and specify the selector
//as the second argument 
$(document).on("click",".tab-content .btn-edit",function(){
    console.log('edit');
});

希望这有助于查看文档中的jquerys以获得更多帮助。

在绑定处理程序时,“.tab content”元素必须位于DOM中。并修复您的选择器或类名
btn_edit
landing_content
对于类/id名称无效,因为在绑定处理程序时下划线无效/不受支持(某些版本中奇怪的情况除外),“.tab content”元素必须位于DOM中。并修复您的选择器或类名
btn_edit
landing_内容
对类/id名称无效,因为下划线无效/不受支持(某些版本中奇怪的情况除外)@CasperSlynge您有什么遗憾吗SIt是为了上面的答案。@Casperlynge你对什么感到抱歉?:坐下来就是为了上面的答案。哈哈哈:我很抱歉。有一双新眼睛总是很好的。有时你会在所有的代码中迷失方向。@CasperSlynge-不用担心,有时候你越是盯着自己的代码看,它看起来就越正确,弄清楚它为什么会被破坏就越令人沮丧。这就是StackOverflow的用途。:-)哈哈哈:我很抱歉。有一双新眼睛总是很好的。有时你会在所有的代码中迷失方向。@CasperSlynge-不用担心,有时候你越是盯着自己的代码看,它看起来就越正确,弄清楚它为什么会被破坏就越令人沮丧。这就是StackOverflow的用途。:-)