gpt4 book ai didi

javascript - 为什么我的 find ('a' ) 在 jquery 中不起作用?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:32:09 25 4
gpt4 key购买 nike

我在 jquery 中有一个函数:

函数 handleUpcomingUserTexts () {

$.ajax({
url: 'getjson.php',
type: "POST",
data: {
mail: '<?php echo htmlentities($_SESSION["email"]); ?>'
},
dataType:'text',
success: function(ans)
{
var data = JSON.parse(ans);

$.each(data, function(i, v) {

var upcomingText = $('<a href="#" class="list-group-item"><i class="fa fa-comment fa-fw"></i> '+v.Content+'<span class="pull-right text-muted small"><em>'+v.Date+'</em></span></a>');


$('#upcomingTexts').append(upcomingText);


var a = upcomingText.find('a').on('click',function(e){

e.preventDefault();
alert("here");


});

});

}});

};

它很好地填充了 html,但是当我单击链接时 - 没有任何反应,我也没有看到警告消息。这里有什么问题?

最佳答案

因为 upcomingText已经是 a .而你正试图找到一个 a在你的a .

你可以这样写:

var a = upcomingText.on('click',function(e){

当你写 var variable = $('<a ...>...</a>'); ,结果是给定 html 的根标签。所以在你的情况下,<a> .

关于javascript - 为什么我的 find ('a' ) 在 jquery 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33417018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com