jQuery通过parent()和parents()方法访问父级元素


<div class="inputGroup">
<p>2.您的最高学历是?</p>
<label><input type="radio" name="edu" value="5"/>高中</label>
<label><input type="radio" name="edu" value="15"/>一年制专科</label>
<label><input type="radio" name="edu" value="19"/>两年制专科</label>
<label><input type="radio" name="edu" value="21"/>三年制以上专科或本科</label>
<label><input type="radio" name="edu" value="22"/>双学位,其中一个受教育年限要大于三年</label>
<label><input type="radio" name="edu" value="23"/>硕士</label>
<label><input type="radio" name="edu" value="25"/>博士</label>
</div>
var input = $("input[type='radio']:checked");
var question = $(input[j]).parents(".inputGroup").children("p").text();
var str = $(input[j]).parent("label").text(); //jQuery对象和Dom对象的转换
alert(question+":"+str);

输出:2.您的最高学历是?:三年制以上专科或本科

parent是指取得一个包含着所有匹配元素的唯一父元素的元素集合。
parents则是取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素)。可以通过一个可选的表达式进行筛选。
可以看出parent取的很明确,就是当前元素的父元素;parents则是当前元素的祖先元素。
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM