作者TonyQ (沉默是金。)
看板Ajax
标题Re: [问题] jQuery取变数一问
时间Mon Mar 7 15:52:34 2011
※ 引述《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)
1F:推 chonhan:原来 each 这麽好用! 受教了 感谢! :) 03/07 15:57
※ 编辑: TonyQ 来自: 72.21.245.243 (03/07 17:05)