jsGears.com 技術論壇 - AJAX, JavaScript, jQuery, 網站開發, 前端效能優化's Archiver

awelkije 發表於 2010-5-4 21:52

請問要如何選取一個元素下面的id值呢?

<div id="a">
<img src="abc.jpg" id="b" />
</div>[code]$("#a > img");[/code]我只能選到下一層的物件
不知該如何取出id值b呢

kbslave 發表於 2010-5-4 23:38

下面是你要的[code]$("#a > img").attr('id') [/code]

awelkije 發表於 2010-5-5 10:14

請問要如何使用關鍵字this呢[code]  $("this > img").attr('id')   [/code]不行[code]  $(this > img).attr('id')[/code]也不行

wmh 發表於 2010-5-5 13:42

$(this) 是在 jQuery 的 callback function 裡面用的

[code js]
$("#a > img").click(function () {
  alert($(this).attr("id"));
});
[/code]

頁: [1]

Powered by Discuz! Archiver  © 2001-2009 Comsenz Inc.