我有一个插件生成的代码,我想捕获click事件并确定单击了哪个特定链接。
<div id="table_1_wrapper" class="wpDataTables wpDataTablesWrapper">
<div class="dt-buttons">
<a class="dt-button new_table_entry DTTT_button DTTT_button_new" tabindex="0" aria-controls="table_1" href="#"><span>New entry</span></a>
<a class="dt-button edit_table DTTT_button DTTT_button_edit" tabindex="0" aria-controls="table_1" href="#"><span>Edit</span></a>
</div>
我能够使用下面的代码捕获click事件,现在我的问题是要检查单击的Anchor标记的跨度内的Text是什么,以便我可以根据单击的链接来执行代码。
document.addEventListener('click', function(e) {
var tre = e.target.closest('a').href || '';
console.log('Clicked');
}, false);
最佳答案
在jQuery / javascript中,您可以通过onClick事件获得此信息:
$(document).on('click', '.dt-button', function (e) {
// you can perform click event on DTTT_button class
// get text of clicked element
var clicked_href_val = $(this).text();
console.log(clicked_href_val); // print value
//var clicked_href_val = $(this).attr('href');
// you can get other parameters(like aria-controls, tabindex whatever defined) of clicked element
});
希望能帮助到你。