在javascript中将值放入动态变量时出现问题
selectedsong div具有不同的链接,链接的关系为differents rel=。。。问题是 我用的是:在javascript中将值放入动态变量时出现问题,javascript,jquery,variables,Javascript,Jquery,Variables,selectedsong div具有不同的链接,链接的关系为differents rel=。。。问题是 我用的是: selectedBtn = $('#selectedsong a'); selectedBtn.click(function() { self.selectedsong($(this).attr('rel')); return false; }); selectedsong: function(number) { $('#selectedsong').ht
selectedBtn = $('#selectedsong a');
selectedBtn.click(function()
{
self.selectedsong($(this).attr('rel'));
return false;
});
selectedsong: function(number)
{
$('#selectedsong').html('new content with new links, rel, and more...');
selectedBtn = $('#selectedsong a'); <---- THE PROBLEM IS HERE,
}
问题是,在第一次单击时,它工作正常,但当selectedsong内容更改时,selectedBtn=$'selectedsong a';无法正常工作,因为selectedBtn.clickfunction无法工作:'
多谢各位 使用
$('#selectedsong').on('click','a',function(e)
{
e.preventDefault(); // prevent default behavior of anchor click
self.selectedsong($(this).attr('rel'));
//return false; dont use return false as it does more work than you need
});
selectedsong: function(number)
{
$('#selectedsong').html('new content with new links, rel, and more...');
}
随着HTML内容的更改,您需要使用事件委派
阅读有关使用的更多信息
$('#selectedsong').on('click','a',function(e)
{
e.preventDefault(); // prevent default behavior of anchor click
self.selectedsong($(this).attr('rel'));
//return false; dont use return false as it does more work than you need
});
selectedsong: function(number)
{
$('#selectedsong').html('new content with new links, rel, and more...');
}
随着HTML内容的更改,您需要使用事件委派
阅读更多关于我认为问题在于您更改了selectedsong中的html,并从中删除了您的标记,这就是您尝试执行的操作selectedsong中的标记。也许你可以试着选择selectedsong而不是anchor标签?或者在更改html时,更改所选歌曲a的html 我认为问题在于您更改了selectedsong中的html,并从中删除了您的标记,这就是您试图执行的操作selectedsong中的标记。也许你可以试着选择selectedsong而不是anchor标签?或者在更改html时,更改所选歌曲a的html 您需要使用.on,因为您正在修改.innerHTML。您需要使用.on,因为您正在修改.innerHTML。