一、总结
jQuery中的parent()的作用是返回的是其上一级的父元素的jQuery对象输出;
jQuery中的parents()的作用是返回的是其全部的父元素的jQuery对象输出;
二、案例
一、如上图中的body中标签的代码:
<form>
<input />
<input type="radio" value="1"/>男 <input type="radio" value="2"/>女
<input type="submit"/>
</form>
<script>
console.log($("[type='radio']").parent());
</script>
结果中含有但是仅仅一个:
二、如上图中的body中标签的代码:
<form>
<input />
<input type="radio" value="1"/>男 <input type="radio" value="2"/>女
<input type="submit"/>
</form>
<script>
$("[type='radio']").parents().each(function(){
console.log(this);
});
</script>
执行的结果如下:由于使用的each方法使得输出的是dom元素