Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中将值放入动态变量时出现问题_Javascript_Jquery_Variables - Fatal编程技术网

在javascript中将值放入动态变量时出现问题

在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

selectedsong div具有不同的链接,链接的关系为differents rel=。。。问题是

我用的是:

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。