Re: [問題] jQuery取變數一問
※ 引述《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
03/07 15:57, 1F
※ 編輯: TonyQ 來自: 72.21.245.243 (03/07 17:05)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章