我具有由插件生成的代码,我想捕获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);
答案 0 :(得分:1)
在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
});
希望有帮助。
答案 1 :(得分:0)
尝试一下,它将为您提供帮助。
<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>
</div>
<script>
document.addEventListener('click', function (e) {
var mtar = e.target.closest('a');
var tre = e.target.closest('a').href || '';
console.log('Clicked' + mtar.innerHTML);
}, false);
</script>
答案 2 :(得分:0)
您可以使用a
轻松地在hasClass()
标记上检查类,
$(this).hasClass('DTTT_button_new')
下面是从点击的元素及其href中获取文本的演示:
$('body').on('click', '.dt-buttons a', function(e) {
e.preventDefault();
console.log($(this).text(), $(this).prop('href'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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="https://add_new_entry"><span>New entry</span></a>
<a class="dt-button edit_table DTTT_button DTTT_button_edit" tabindex="0" aria-controls="table_1" href="https://edit_link"><span>Edit</span></a>
</div>
答案 3 :(得分:0)
您可以使用onclick
关键字发送元素this
。
function a(e)
{
console.log($(e).attr('href'))
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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="#a" onclick="a(this)"><span>New entry</span></a>
<a class="dt-button edit_table DTTT_button DTTT_button_edit" tabindex="0" aria-controls="table_1" href="#b" onclick="a(this)"><span>Edit</span></a>
</div>
答案 4 :(得分:0)
尝试一下,它将为您提供确切的文字。
<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>
</div>
<script>
document.addEventListener('click', function (e) {
var mtar = e.target.closest('span');
var tre = e.target.closest('a').href || '';
console.log('Clicked Text: ' + mtar.innerHTML);
}, false);
</script>