jQuery onClick 阻止默认
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18079656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery onClick preventDefault
提问by ShadowFlame
I have a table of which I want to suppress the link clicks, because I need the links for other functionality.
我有一个表格,我想抑制链接点击,因为我需要其他功能的链接。
The structure of the table is something like this:
表的结构是这样的:
<table>
<tr><th>Day</th><th>Event</th>
<tr class="DAY" id="DAY_0"><td>1-8-2013</td><td><a href="?tab=tabCalendar&dayEvent=DAY_0">Add Event</a></td></tr>
<tr class="DAY" id="DAY_1"><td>2-8-2013</td><td><a href="?tab=tabCalendar&dayEvent=DAY_1">Add Event</a></td></tr>
</table
my jquery code to try and block the from refreshing the page, and showing the id is this
我的 jquery 代码尝试阻止刷新页面,并显示 id 是这样的
<script>
$("a").click(
function(event) {
event.preventDefault();
alert('Picked: '+ event.target.id.slice(4) );
}
);
</script>
I have also tried the following
我也试过以下
$(".DAY").click(function(){//to catch the class DAY.click()
and even
乃至
$("[id^=DAY]").click(function(){//to catch the id DAY*.click
however, none of these function did anything.
然而,这些功能都没有做任何事情。
The versions I use are
我使用的版本是
jquery-1.9.1.js
jquery-ui-1.10.3.custom.js
采纳答案by Rushil Sablania
Simplest approach:
最简单的方法:
<script>
$(".DAY").click(
function(event) {
event.preventDefault();
alert('Picked: '+ $(this).attr('id').slice(4));
}
);
</script>
回答by Gautam3164
Try to put the script in DOM READY
尝试将脚本放入 DOM READY
<script type="text/javascript">
$(document).ready(function(){
$("a").on('click',function(event) {
event.preventDefault();
alert('Picked: '+ $(this).parent('td').parent('tr').id.slice(4) );
});
});
</script>
回答by ajtrichards
Try this:
尝试这个:
$(document).ready(function(){
$("a").click(
function(event) {
event.preventDefault();
DATA = $(this).closest('tr').attr('id').slice(4);
alert('Picked: ' + DATA);
}
);
});
This will get select the ID
of the a
you clicked. I've also created this as a jSFiddle: http://jsfiddle.net/pAZeb/
这将让选择ID
的a
,你点击。我也将它创建为 jSFiddle:http: //jsfiddle.net/pAZeb/
回答by Arun P Johny
回答by Thanassis_K
Maybe you need to modify to the following:
也许您需要修改为以下内容:
$("a").click(
function (event) {
event.preventDefault();
alert('Picked: ' + $(this).closest('tr').attr('id').slice(4));
});