Re: [問題] jQuery取變數一問

看板Ajax作者 (沉默是金。)時間14年前 (2011/03/07 15:52), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《chonhan ()》之銘言: : 不好意思... 小弟是 jQuery 新手 : 想問一個取變數的問題 : 我的 html 中若有 n 個 class 為 course 的 div tag : 當中的每個 tag 內 又各自有 數量不等的 class 為 subCourse 的 div tag : 有點像是以下這樣 : <div class='course' id='1'> : <div class='subCourse' id='a'>XXX</div> : <div class='subCourse' id='b'>YYY</div> : </div> : <div class='course' id='2'> : <div class='subCourse' id='c'>ZZZ</div> : </div> : 我只知道 取得 每個 .course 的方法是 : <script> : var element = $('.myCourse'); : for (var i = 0; i < element.length; i++) { : element[i].id; : } : </script> : 但我該怎麼取得 element[i] 內的 .subCourse id 呢? : 還請各位高手指教 謝謝 :) var $elements = $(".myCourse"); $elements.each(function(){ //"this" is a dom , not jQuery context alert(this.id); //id of current course //get the sub course in current course var $subs= $(".sibCourse",this); //use closure to keep the current course in each. var that = this; //iterate all the sub course in current course $subs.each(function(){ //alert current course and current sub course alert(that.id +":"+this.id); }); }); -- 我:一半的日子讓你說,我聽你說你的所有______________________________________ ______________________________________一半的日子我想說,對你說過去的所有:我 _______________________________________________________ 在討論中妥善扮演兼具聆聽與分享的角色,是我們一生的課題。 _______________________________________________________ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 72.21.245.243 ※ 編輯: TonyQ 來自: 72.21.245.243 (03/07 15:53)

03/07 15:57, , 1F
原來 each 這麼好用! 受教了 感謝! :)
03/07 15:57, 1F
※ 編輯: TonyQ 來自: 72.21.245.243 (03/07 17:05)
文章代碼(AID): #1DT8x5Yk (Ajax)
討論串 (同標題文章)
文章代碼(AID): #1DT8x5Yk (Ajax)