$('td').click(function() {
myId = $(this).parent().attr("id");
myItem = $(this).text();
alert('u clicked ' + myId);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
<tr>
<td>Computer</td>
</tr>
<tr>
<td>maths</td>
</tr>
<tr>
<td>physics</td></div>
</tr>
</table>
当我点击表项我想警告'u clicked science'
但我得到undefined
。
任何帮助,将不胜感激!
尝试,警报( 'U点击' + myItem); – Ravneet
为什么我在html中看不到任何'id's? – Rajesh
请编辑您的问题以显示外部html - 您当前的代码不显示任何id属性或任何地方的字符串“science”。我想那包含在一个parenet元素中,但要回答我需要看到这个元素 – Steve