我在我的html正文中有以下代碼。如何獲取在jquery中選擇的圖像的ID
<ul>
<li>
<div>
<a href=javascript:contactsAction() >
<img src="contactimageloc1.jpg" id="image_id1 " width="140px" height="160px"/>
</a>
<br>
firstName lastName , title
</div>
</li>
<li>
<div>
<a href=javascript:contactsAction() >
<img src="contactimageloc.jpg" id="image_id2" width="140px" height="160px"/>
</a>
<br>
firstName lastName , title
</div>
</li>
</ul>
當有人點擊圖像時,我想知道圖像ID。
<script>
function contactsAction(){
alert('On Image click function'+$(':image'));
alert('Inside $(this).find(a:first).attr(id) :'+$(this).find('a:first').attr('id'));
alert('image $(this).children(a).attr(id): '+$(this).children('a').attr('id'));
alert('image $(this).children(a).eq(0).attr(id): '+$(this).children('a').eq(0).attr('id'));
alert('$(this).children().length: '+ $(this).children().length);
alert('image id: '+$(this).attr('id').value);
}
</script>
對於alert('$(this).children().length: '+ $(this).children().length);
我得到的輸出爲0
而對於所有其他警報我收到未定義作爲輸出。
任何人都可以幫助我如何獲得所選圖像的圖像ID。
感謝
你對長度做了什麼? – Mike 2012-01-30 10:52:25
請正確縮進您的代碼,以便其他人可以閱讀它。 – 2012-01-30 10:55:10