Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery根据表单元格的id选择表单元格_Javascript_Jquery - Fatal编程技术网

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)
没有任何意义。这样的点击没有属性。