-
03-10-2019 - |
题
好吧,经过无数的尝试,我无法获得这项工作吗?
<script type="text/javascript">
$("td input").focusout(function() {
var column = $(this).parent('td').attr('class');
var row = $(this).parent('tr').attr('id');
$('#dat').HTML(row+" "+column);
});
</script>
HTML看起来像这样:
<tr class="numbers" id="1">
<td class="a" align="right">1</td>
<td class="b"><input class="input" type="text" value=""/></td>
<td class="c"><input class="input" type="text" value=""/></td>
<td class="d"><input class="input" type="text" value=""/></td>
<td class="e"><input class="input" type="text" value=""/></td>
<td class="f">0</td>
<td class="g"><input class="input" type="text" value=""/></td>
</tr>
谁能将我指向正确的方向?
解决方案
注意应该是小写
$('#dat').HTML(row+" "+column);
.html
和
$(this).parent('tr')
是空的,输入不能有父母tr
另外,您可以使用
其他提示
尝试:
$(this).closest('td').attr('class')
在您的代码中的适当位置。 ID同样的事情。
您的代码在大多数情况下看起来正确,只需修复以下内容:
$('#dat').HTML(row+' ' +column);
为此:
$('#dat').html(row + ' ' + column);
还有这个:
$(this).parent('tr').attr('id');
为此:
$(this).parents('tr').attr('id');
您想使用 .parents()
功能而不是 .parent()
.
从文档中出发的原因是我的突出显示。
给定一个代表一组DOM元素的jQuery对象,.parent()方法使我们能够通过DOM树中的这些元素搜索这些元素,并从匹配元素中构造一个新的jQuery对象。 .parents()和.parent()方法相似,只是后者仅在DOM树上传播一个级别。
不隶属于 StackOverflow