Javascript 使用jquery根据表单元格的id选择表单元格
我把桌子摆好了Javascript 使用jquery根据表单元格的id选择表单元格,javascript,jquery,Javascript,Jquery,我把桌子摆好了 <table class="tab1" width="400" cellpadding="20"> <tr> <td id="cell1">Name</td> <td id="cell2">Company</td> </tr> </table> 名称 单位 正在使用 <script type="text/javascr
<table class="tab1" width="400" cellpadding="20">
<tr>
<td id="cell1">Name</td>
<td id="cell2">Company</td>
</tr>
</table>
名称
单位
正在使用
<script type="text/javascript">
$(document).ready(function(){
if($('#cell1').attr('click',true)) {
alert("Cell 1 was clicked");
}
});
</script>
$(文档).ready(函数(){
if($('#cell1').attr('click',true)){
警报(“已单击单元格1”);
}
});
我希望在单击cell1时显示警报消息。目前我没有得到任何输出 试试看:
$('td').click(function(){
if(this.id=='cell1') alert('cell1 clicked');
});
您需要使用click事件,而不是属性(attr()
)。根据需要,您也可以使用$('#cell1')直接绑定到ID为cell1的单元格。单击(function(){…})
尝试:
$('td').click(function(){
if(this.id=='cell1') alert('cell1 clicked');
});
您需要使用click事件,而不是属性(attr()
)。根据需要,您也可以使用$('#cell1')直接绑定到ID为cell1的单元格。单击(function(){…})
试试
$("td#cell1").click(function() {
alert("Cell 1 was clicked")
});
试一试
jquery选择器必须选择适当的单元格并将onclick处理程序附加到该单元格,如下所示:
$('td#cell1').on('click', function(){
alert('cell1 clicked');
});
jquery选择器必须选择适当的单元格并将onclick处理程序附加到该单元格,如下所示:
$('td#cell1').on('click', function(){
alert('cell1 clicked');
});
由于它是一个
ID
我会直接附加它,attr
不是您想要的
$('#cell1').click(function(){
alert('Cell1 has been clicked!');
});'
希望这有帮助 由于它是一个
ID
我会直接附加它,attr
不是您想要的
$('#cell1').click(function(){
alert('Cell1 has been clicked!');
});'
希望这有帮助 在您的
jQuery
代码中,您需要将元素的ID
作为目标,如下所示:
$('#cell1').click(function(){
alert('cell1 was clicked');
});
如果您想更全面地确定目标,请这样做:
$('#my-table td').click(function(){
alert($(this).attr('name')+' was clicked');
});
使用此HTML
代码:
<table id="my-table" class="tab1" width="400" cellpadding="20">
<tr>
<td name="cell1">Name</td>
<td name="cell2">Company</td>
</tr>
</table>
名称
单位
使用第二种方法,您可以在
jQuery
代码中分解LI
单击的绑定,您需要以元素的ID
为目标,如下所示:
$('#cell1').click(function(){
alert('cell1 was clicked');
});
如果您想更全面地确定目标,请这样做:
$('#my-table td').click(function(){
alert($(this).attr('name')+' was clicked');
});
使用此HTML
代码:
<table id="my-table" class="tab1" width="400" cellpadding="20">
<tr>
<td name="cell1">Name</td>
<td name="cell2">Company</td>
</tr>
</table>
名称
单位
使用第二种方法,您可以将LI
clicks的绑定分解。attr(“click”,true)
没有任何意义。这样的点击没有属性。.attr(“点击”,true)
没有任何意义。这样的点击没有属性。