gpt4 book ai didi

javascript - 我如何知道哪个 anchor 标记被点击?

转载 作者:行者123 更新时间:2023-11-28 16:25:06 25 4
gpt4 key购买 nike

我正在动态创建内容,当单击 anchor 标记时,将调用 navigate() 函数。我如何确定点击了哪个 anchor 标签?

    function navigate()
{
//var location=$(this).attr("id");
switch(location)
{
/*case "Configuration": $('#detailTable').empty();
$('#detailTable').append(navigateConfig);
break;*/

default: alert(location+" a tag was clicked");
}
}

$('#detailTable').empty();
$('<div width="100%">')
.attr('id','javainfoSpan')
.html('<div class="titleBlue"><a href="javascript:navigate();">Configuration</a>&gt;'+productname+'&gt;Java Properties</div>'+
//some other stuff
'</div>')
.appendTo('#detailTable');

更新:

我的问题很简单

  1. detailTable 内会有许多 a 元素。
  2. 如何在某些 js 函数中了解点击了哪个 a 元素?

最佳答案

我明白您现在在做什么,您正在动态创建 a 元素。如果您调用 javascript:navigate() 那么您使用的是标准 javascript,而不是 jquery(需要 jquery 才能使用 $(this) 选择器)。

相反,你应该有这个:

$("body").on('click', 'a', function() {
var location = $(this).attr('id');
switch(location){ /* ... */ }
});

这将捕获 jquery 中任何您动态创建的元素或页面加载时已经存在的元素的点击事件。

请记住,如果您使用 id,那么您需要在 a 元素上设置 id 属性。

关于javascript - 我如何知道哪个 anchor 标记被点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8294712/

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