Javascript 使用jQuery双击表行时打开链接
我有一个Javascript 使用jQuery双击表行时打开链接,javascript,jquery,html,double-click,Javascript,Jquery,Html,Double Click,我有一个表,看起来像这样: <table id="table"> <thead> <tr class='tablehead'> <th>Test</th> </tr> </thead> <tbody> <tr class='tablecell'> <td> </td
表
,看起来像这样:
<table id="table">
<thead>
<tr class='tablehead'>
<th>Test</th>
</tr>
</thead>
<tbody>
<tr class='tablecell'>
<td>
</td>
</tr>
</tbody>
</table>
$('tr').dblclick(function(){
var id = $(this).attr('id');
//do something with id
})
$(document).ready(function() {
$('.tablecell').click(function() {
return false;
}).dblclick(function() {
window.open("your_url");
return false;
});
});
试验
<tr id="something">...</tr>
工作演示:(根据您提供的示例代码创建)
dblclick
api$(this).attr('id')
来获取id,显然您将在标记中定义id$(document).ready(function() {
$('#table >thead > tr').dblclick(function() {
alert('Row dblclicked');
alert($(this).attr('class'));
});
});
你的意思是这样的:
<table id="table">
<thead>
<tr class='tablehead'>
<th>Test</th>
</tr>
</thead>
<tbody>
<tr class='tablecell'>
<td>
</td>
</tr>
</tbody>
</table>
$('tr').dblclick(function(){
var id = $(this).attr('id');
//do something with id
})
$(document).ready(function() {
$('.tablecell').click(function() {
return false;
}).dblclick(function() {
window.open("your_url");
return false;
});
});
您可以创建一个隐藏字段,并在双击时用id填充该字段。这可能会帮助您:
jQuery(function($) {
$('#table tr').click(function() {
return false;
}).dblclick(function() {
window.location = url;
return false;
});
});
我认为这是一个很好的方法,但在这个阶段,dooonot可能会遇到一些复杂的问题。请记住,要使用jQuery,必须在head标记html代码中声明库。与任何其他javascript文件一样。当然,你必须在下载之前下载。是的,你是对的,但不幸的是,我只能接受一个。正如预期的那样:)