怎么得到下面的节点 看图,当我点击这个红色圈圈的这个按钮时候,得到它所在行的第一个节点,即是它所在行的那个复选框。这个js这么写?或者是jquery ,注意 三个按钮没有id属性。至于为什么没有,他有道理的。不能有id属性,所以不能 $("#id").。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript"> function change(x){ var tr=x.parentNode.parentNode; var c=tr.firstChild; if(!c.tagName){ c=c.nextSibling; } c=c.firstChild; if(c.checked==true){ c.checked=false; }else{ c.checked=true; } }</script></head><body><table> <tr> <td><input type="checkbox" /></td> <td>1</td> <td><input type="button" value="change" onclick="change(this)"></td> </tr> <tr> <td><input type="checkbox" /></td> <td>1</td> <td><input type="button" value="change" onclick="change(this)"></td> </tr> <tr> <td><input type="checkbox" /></td> <td>1</td> <td><input type="button" value="change" onclick="change(this)"></td> </tr></table></body></html>类似这样试试 $(':button').click(function(){ alert($(this).parents('tr').children(':first').children(':first').val()); }); 给个jQuery的简便操作$(":button").click(function(){ var chk = $(this).parents("tr").find("td:first :checkbox"); console.log(chk.val());}); 不能有ID,那如果可以有class也好办,如果连class也没有,那就用tagName的方式,楼上的方法是可以的。 一般没有id的时候就通过其它的属性来去元素,或者考虑是否可以用this,只要得到当前操作的元素,都可以通过层级关系来获取其它的元素,具体的实例前面的大婶都已经给出了 获取元素的方法很多种的,不止只有id可以获取,js本身就支持根据name,或者标签来获取建议看看jquery的api,看看如何获取自身元素,如何获取兄弟姐妹,父元素等等,看看选择器任何组合的 IE下在file input上点击回车键,为何无法弹出对话框? 是不是安全的问题? 如何使用javascript与cookie保存层的拖动后位置?? 为什么这个东西这能在本地运行呢?? 如何防止控件从不可见到可见时把表格撑开? web小技巧問題 求助:如何用javascript打开一个新窗口,具体要求见内容 谁帮我看看这个。。。 说能告诉我这样得效果怎么实现 请问如何判断是否处于汉字输入状态?(内空) jqGrid select类型搜索BUG ??? 请教关于使用appscan 检测"基于DOM的跨站点脚本"漏洞修复问题
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function change(x){
var tr=x.parentNode.parentNode;
var c=tr.firstChild;
if(!c.tagName){
c=c.nextSibling;
}
c=c.firstChild;
if(c.checked==true){
c.checked=false;
}else{
c.checked=true;
}
}
</script>
</head><body>
<table>
<tr>
<td><input type="checkbox" /></td>
<td>1</td>
<td><input type="button" value="change" onclick="change(this)"></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>1</td>
<td><input type="button" value="change" onclick="change(this)"></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>1</td>
<td><input type="button" value="change" onclick="change(this)"></td>
</tr>
</table>
</body>
</html>
类似这样试试
alert($(this).parents('tr').children(':first').children(':first').val());
});
$(":button").click(function(){
var chk = $(this).parents("tr").find("td:first :checkbox");
console.log(chk.val());
});
建议看看jquery的api,看看如何获取自身元素,如何获取兄弟姐妹,父元素等等,看看选择器任何组合的