Javascript 单击动态生成的锚标记时获取超链接文本

Javascript 单击动态生成的锚标记时获取超链接文本,javascript,html,python-2.7,sqlite,Javascript,Html,Python 2.7,Sqlite,我正在尝试使用html模板文件上的瓶子显示sqllite表的内容。这是模板文件中的处理程序代码 %for row in rows: <tr> %for col in row[0::2]: <td><div class="box"><a class="button" href="#popup1" id ="a1">{{col}}</a></div></td> %end %end 有人

我正在尝试使用html模板文件上的瓶子显示sqllite表的内容。这是模板文件中的处理程序代码

%for row in rows:
    <tr>
  %for col in row[0::2]:
        <td><div class="box"><a class="button" href="#popup1" id ="a1">{{col}}</a></div></td>
 %end
%end

有人能建议我如何获得这种动态生成的锚定标记的超链接文本吗?我的目标是在单击时读取超链接的文本并发送回我的python代码,在那里我将使用它作为sql查询的参数来呈现一些其他输出。

as标识符必须是唯一的。您不能分配重复的ID。CSS类可用于绑定事件处理程序。在这里的代码片段中,我将
a1
类添加到锚点

%for row in rows:
    <tr>
  %for col in row[0::2]:
        <td><div class="box"><a class="button a1" href="#popup1">{{col}}</a></div></td>
 %end
%end

您应该使用最近的静态容器来代替
文档。

作为标识符必须是唯一的。您不能分配重复的ID。CSS类可用于绑定事件处理程序。在这里的代码片段中,我将
a1
类添加到锚点

%for row in rows:
    <tr>
  %for col in row[0::2]:
        <td><div class="box"><a class="button a1" href="#popup1">{{col}}</a></div></td>
 %end
%end

代替
文档
您应该使用最近的静态容器。

当您单击链接时。按钮类事件发生

$(document).on('click', ".button", function(){
   alert($(this).text())
});

当你点击链接时。按钮类事件发生

$(document).on('click', ".button", function(){
   alert($(this).text())
});

1) id必须是唯一的,2)第二个不会触发,因为你没有
标签。@KevinKloet:谢谢Kevin,我在寻找这种行为的根本原因。你能告诉我如何为这些代码获取唯一的ID吗?在这种情况下,你根本不需要ID,你可以使用一个类来对它们进行分组,例如,如果你给它们类“anchorClass”,你可以做一些类似
$('.anchorClass')。单击(函数(){window.alert($(this.text());})1)id必须是唯一的,2)第二个id永远不会触发,因为您没有
标记。@KevinKloet:谢谢Kevin,我在寻找这种行为的根本原因。你能告诉我如何为这些代码获取唯一的ID吗?在这种情况下,你根本不需要ID,你可以使用一个类来对它们进行分组,例如,如果你给它们类“anchorClass”,你可以做一些类似
$('.anchorClass')。单击(函数(){window.alert($(this.text());})
$(document).on('click', ".a1", function(){
    alert($(this).text())
});
$(document).on('click', ".button", function(){
   alert($(this).text())
});