Javascript 取消列级链接时防止行级链接
我有一张桌子Javascript 取消列级链接时防止行级链接,javascript,jquery,Javascript,Jquery,我有一张桌子 <table class="data-table"> <thead> <tr> <th width="16">ID</th> <th>Name</th> <th>Reference</th> <th>Link</th> <th>YouTube</th>
<table class="data-table">
<thead>
<tr>
<th width="16">ID</th>
<th>Name</th>
<th>Reference</th>
<th>Link</th>
<th>YouTube</th>
<th>Status</th>
<th width="16">Delete</th>
</tr>
</thead>
<tbody>
<tr class="clickable" data-href="/videos/1/">
<td>1</td>
<td>Test Video</td>
<td></td>
<td></td>
<td></td>
<td>
<a href="/videos/delete/1"
onclick="return confirm_delete();">
Delete
</a>
</td>
</tr>
</tbody>
</table>
剧本是
<script>
function confirm_delete() {
var result = confirm("Are you sure you want to delete this video?");
return result;
}
</script>
单击该行将转到正确的URL。
单击删除链接要求确认。如果选择“确定”,则会转到正确的删除URL,但如果选择“取消”,则会取消删除URL,但转到行级URL。我怎样才能防止呢
<a href="/videos/delete/1"
onclick="event.stopPropogation(); return confirm_delete();">Delete</a>
不过,请帮自己一个忙,将javascript放在脚本标记中
<a id="delete_1" href="/videos/delete/1">Delete</a>
...
<script>
$('#delete_1').click(function(e) {
e.stopPropogation();
return confirm("Are you sure you want to delete this video?");
});
</script>
感谢所有评论/回答的人。为了解决这个问题,我做了以下操作 首先,我删除了onclick属性,并向a标记添加了一个类 然后我做了下面的脚本
<script>
$(document).ready(function() {
$('a.delete').click(function(event) {
var result = confirm("Are you sure you want to delete this video?");
if (!result) {
event.preventDefault();
event.stopPropagation();
}
});
});
</script>
如果问题是来自锚元素的事件传播,请使用event.stopPropagation阻止它。请参阅
<script>
$(document).ready(function() {
$('a.delete').click(function(event) {
var result = confirm("Are you sure you want to delete this video?");
if (!result) {
event.preventDefault();
event.stopPropagation();
}
});
});
</script>