JQuery DOM - 仍由旧id引用的元素

时间:2013-01-13 02:06:37

标签: jquery dom

我正在使用此功能更改模态按钮的ID

$('#editRevision a').click(function()
{   
    //clear the uploadify queue
    $('#file_upload').uploadifive('clearQueue');

    //change the id of the footer next button to pagesOrdered
    $('#next').attr('id' , 'pagesOrdered');
});

只是为了检查'.on('click',function())是否正常工作我使用以下

//Pages ordered click
$('#pagesOrdered').on('click',function()
{
     alert('Testing');
});

我的提醒未触发,但如果我将#pagesOrdered更改回#next则会触发。从DOM中释放#next我需要做什么。

谢谢!

1 个答案:

答案 0 :(得分:2)

调用代码时,

$('#pagesOrdered')不存在。您可以在更改id或将on()委托给更高级别的DOM树并使用#pagesOrdered作为选择器

后添加点击处理程序

第一种方法:

$('#editRevision a').click(function(){   
    //clear the uploadify queue
    $('#file_upload').uploadifive('clearQueue');

    //change the id of the footer next button to pagesOrdered
    $('#next').attr('id' , 'pagesOrdered');
    //Pages ordered click
    $('#pagesOrdered').on('click',function(){
     alert('Testing');
     });
});

第二种方法:

 $(document).on('click','#pagesOrdered',function(){
     alert('Testing');
 });

在更改#next之前,您还应解除附加到id的事件的绑定。更改id不会删除它们。在不改变id的情况下,比如切换类并在处理程序中运行类特定代码,可能是一种更简单的方法来处理你正在做的事情