我需要获取属性值,但它说它未定义。
$(document).ready(function() {
$(".try").click(function() {
alert($(this).attr("value"));
});
});
<table id="try" class="try" cellspacing="0" cellpadding="0">
<td style="background:#80FF80;" value="WD">WD</td>
<td style="background:#FFFF80;" value="RD">RD</td>
<td style="background:#879FDE;" valu ="OD">OD</td>
</tr>
</table>
最佳答案
您必须将子元素包含到您的选择器中。您的 html 也不正确。您缺少打开 tr
:
$(".try tr td").click(function() {
alert($(this).attr("value"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="try" class="try" cellspacing="0" cellpadding="0">
<tr>
<td style="background:#80FF80;" value="WD">WD</td>
<td style="background:#FFFF80;" value="RD">RD</td>
<td style="background:#879FDE;" value="OD">OD</td>
</tr>
</table>
关于javascript - 警报自定义属性值 jquery 表单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27090219/