[英]Get href of dynamically created bound link
我有一些在表中动态创建的链接,这些链接的href
发送一个GET
请求删除用户。 我有这样的听者绑定:
var $usersTableBody = $('#table-users tbody');
var $deleteUserBtn = $('.delete-user-btn');
$usersTableBody.on('click', $deleteUserBtn, deleteConfirm);
我需要得到href
的$deleteUserBtn
,问题是,现在我不能得到的链接<a>
,我点击,因为该事件被绑定到表身。 那么...我该怎么做呢?
为您轻松
// this argument should be a string
// ↓
$('#table-users tbody').on('click', '.delete-user-btn', function(e) {
alert(this.href); // "this" is the event target / source
});
参见http://api.jquery.com/on/#on-events-selector-data-handler
选择器
类型: 字符串
一个选择器字符串,用于过滤触发事件的所选元素的后代。
$(document).on("click", "a.delete-user-btn", function(event) {
// prevent default action, to not affect any other
// event handlers attached to `a.delete-user-btn`
event.preventDefault();
// do stuff with `this` : `a.delete-user-btn` `href` property
console.log(this.href)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.