Jquery搜索父元素操作方法?
使用js或者jquery查找父元素、子元素经常遇到。可是用起来总容易混淆,这里统一总结了一下,以后用起来相信会方便好多
这里jquery向上查找父元素 用到的方法:
closest() parents() parent()
向下查找子元素
用到的方法:find() children()
js用的是 children[] 属性 !
jquery怎么选取索引大于1的元素?
$.index()是获取这个元素的索引值,俗称“只读”操作,不带“写入”功能 $.eq(index)才是对这个索引的元素进行设置,俗称“只写”操作 换成:$('.nav ul>li').eq(li_index).css({...}) 就行了
jquery中怎样根据父级找元素?
jquery中parent()可以获取父级元素,所以获得某元素父级的父级可以使用
$(selector).parent().parent();
示例如下
创建Html代码及css样式
class1
class2
class3
div{padding:10px 20px;border:4px solid #ebcbbe;}
div.class1{width:200px;height:120px;}
编写jquery代码
$(function(){
$("div.class3").click(function() {
obj = $(this).parent().parent();
alert(obj.prop('class'));
});
})

