js中通过父级parent函数和next函数进行查找定位元素

221 篇文章 5 订阅
178 篇文章 1 订阅

js中通过父级parent函数和find函数进行查找定位元素

parent() 方法

定义和用法

parent() 获得当前匹配元素集合中每个元素的父元素,使用选择器进行筛选是可选的。

详细说明

如果给定一个表示 DOM 元素集合的 jQuery 对象,.parent() 方法允许我们在 DOM 树中搜索这些元素的父元素,并用匹配元素构造一个新的 jQuery 对象。.parents() 和 .parent() 方法类似,不同的是后者沿 DOM 树向上遍历单一层级。

该方法接受可选的选择器表达式,与我们向 $() 函数中传递的参数类型相同。如果应用这个选择器,则将通过检测元素是否匹配该选择器对元素进行筛选。

请思考这个带有基本的嵌套列表的页面:

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="/jquery/jquery.js"></script>
</head>

<body>
<ul class="level-1">
  <li class="item-i">I</li>
  <li class="item-ii">II
    <ul class="level-2">
      <li class="item-a">A</li>
      <li class="item-b">B
        <ul class="level-3">
          <li class="item-1">1</li>
          <li class="item-2">2</li>
          <li class="item-3">3</li>
        </ul>
      </li>
      <li class="item-c">C</li>
    </ul>
  </li>
  <li class="item-iii">III</li>
</ul>

<script>
$('li.item-a').parent().css('background-color', 'red');
</script>

</body>
</html>

在这里插入图片描述此次调用的结果是,为 level-2 列表设置红色背景。由于我们未应用选择器表达式,父元素很自然地成为了对象的一部分。如果已应用选择器,则会在包含元素之前,检测元素是否匹配选择器。

实例代码

<div class="wenku_list">
	<div class="wenku_pic">
		<img class="input-btn_wenku" src="https://www.qipa250.com/WenKu/20210114110029552685780856.jpg" alt="">
	</div>
	<input type="file" class="file_wenku" style="display:none" accept="image/*">
	<input type="hidden" class="wenku_pic_id" name="wenku_pic_id" value="9601">
	<p style="text-align: center;margin-top: 5px">
	第1张图片</p>
</div>

js代码


//点击input-btn_wenku 时,得到父级parent(),使用next得到与父级同级class名称为file_wenku的元素,

$(document).on('click', ".input-btn_wenku", function () {
        //点击上传 找到下一个file_wenku节点
        $(this).parent().next(".file_wenku").click();
});
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
核心 jQuery 核心函数 jQuery(expr, [context]) jQuery(html, [ownerDoc]) jQuery(elements) jQuery(callback) jQuery 对象访问 each(callback) size() length selector context get() get(index) index(subject) 数据缓存 data(name , [value]) removeData(name) queue(name ,[cb|queue]) dequeue(name) 插件机制 jQuery.fn.extend(object) jQuery.extend(object) 多库共存 jQuery.noConflict([extreme]) 属性 属性 attr(name) attr(properties) attr(key, value) attr(key, fn) removeAttr(name) CSS 类 addClass(class) removeClass([class]) toggleClass(class [, switch]) HTML代码 html( [val] ) 文本 text( [val] ) 值 val( [val] ) CSS CSS css(name) css(properties) css(name, value) 位置 offset() position() scrollTop( [val] ) scrollLeft( [val] ) 尺寸 height( [val] ) width( [val] ) innerHeight() innerWidth() outerHeight(options) outerWidth(options) 选择器 基本 #id element .class * selector1,selector2,selectorN 层级 ancestor descendant parent > child prev + next prev ~ siblings 基本 :first :last :not :even :odd :eq :gt :lt :header :animated 内容 :contains :empty :has :parent 可见性 :hidden :visible 属性 [attribute] [attribute=value] [attribute!=value] [attribute^=value] [attribute$=value] [attribute*=value] [attrSel1][attrSel2][attrSelN] 子元素 :nth-child :first-child :last-child :only-child 表单 :input :text :password :radio :checkbox :submit :image :reset :button :file :hidden 表单对象属性 :enabled :disabled :checked :selected 文档处理 内部插入 append(content) appendTo(content) prepend(content) prependTo(content) 外部插入 after(content) before(content) insertAfter(content) insertBefore(content) 包裹 wrap(html) wrap(elem) wrapAll(html) wrapAll(elem) wrapInner(html) wrapInner(elem) 替换 replaceWith(content) replaceAll(selector) 删除 empty() remove([expr]) 复制 clone() clone(true) 筛选 过滤 eq(index) hasClass(class) filter(expr) filter(fn) is(expr) map(callback) not(expr) slice(start, [end]) 查找 add(expr) children([expr]) closest([expr]) contents() find(expr) next([expr]) nextAll([expr]) offsetParent() parent([expr]) parents([expr]) prev([expr]) prevAll([expr]) siblings([expr]) 串联 andSelf() end() 事件 页面载入 ready(fn) 事件处理 bind(type, [data], fn) one(type, [data], fn) trigger(type, [data]) triggerHandler(type, [data]) unbind([type], [data]) 事件委派 live(type, fn) die([type], [fn]) 事件切换 hover(over, out) toggle(fn, fn2, [fn3, fn4, ...]) 事件 blur( [fn] ) change( [fn] ) click( [fn] ) dblclick( [fn] ) error( [fn] ) focus( [fn] ) keydown( [fn] ) keypress( [fn] ) keyup( [fn] ) mousedown(fn) mousemove(fn) mouseout(fn) mouseover(fn) mouseup(fn) resize(fn) scroll(fn) select( [fn] ) submit( [fn] ) unload(fn) 效果 基本 show() show(speed, [callback]) hide() hide(speed, [callback]) toggle() toggle(switch) toggle(speed, [callback]) 滑动 slideDown(speed, [callback]) slideUp(speed, [callback]) slideToggle(speed, [callback]) 淡入淡出 fadeIn(speed, [callback]) fadeOut(speed, [callback]) fadeTo(speed, opacity, [fn]) 自定义 animate(param,[dur],[e],[fn]) animate(params, options) stop([clearQueue], [gotoEnd]) 设置 jQuery.fx.off Ajax Ajax 请求 $.ajax([options]) load(url, [data], [callback]) $.get(url, [data], [fn], [type]) $.getJSON(url, [data], [fn]) $.getScript(url, [callback]) $.post(url, [data], [fn], [type]) Ajax 事件 ajaxComplete(callback) ajaxError(callback) ajaxSend(callback) ajaxStart(callback) ajaxStop(callback) ajaxSuccess(callback) 其它 $.ajaxSetup([options]) serialize() serializeArray() 工具 浏览器及特性检测 $.support $.browser $.browser.version $.boxModel 数组和对象操作 $.each(object, [callback]) $.extend([d],tgt,obj1,[objN]) $.grep(array, fn, [invert]) $.makeArray(obj) $.map(array, callback) $.inArray(value, array) $.merge(first, second) $.unique(array) 测试操作 $.isArray(obj) $.isFunction(obj) 字符串操作 $.trim(str) URL $.param(obj) 关于 关于jQuery 1.3 版翻译 关于jQuery 1.2 版翻译 提交bug及获取更新
JQuery开发手册 jQuery 核心函数 jQuery([sel,[context]]) jQuery(html,[ownerDoc]) jQuery(callback) jQuery.holdReady(hold)1.6+ jQuery 对象访问 each(callback) size() length selector context get([index]) index([selector|element]) 数据缓存 data([key],[value]) removeData([name|list])1.7* $.data(element,[key],[value]) 队列控制 queue(element,[queueName]) dequeue([queueName]) clearQueue([queueName]) 插件机制 jQuery.fn.extend(object) jQuery.extend(object) 多库共存 jQuery.noConflict([ex]) 属性 属性 attr(name|pro|key,val|fn) removeAttr(name) prop(name|pro|key,val|fn)1.6+ removeProp(name)1.6+ CSS 类 addClass(class|fn) removeClass([class|fn]) toggleClass(class|fn[,sw]) HTML代码/文本/值 html([val|fn]) text([val|fn]) val([val|fn|arr]) CSS CSS css(name|pro|[,val|fn]) 位置 offset([coordinates]) position() scrollTop([val]) scrollLeft([val]) 尺寸 heigh([val|fn]) width([val|fn]) innerHeight() innerWidth() outerHeight([soptions]) outerWidth([options]) 选择器 基本 #id element .class * selector1,selector2,selectorN 层级 ancestor descendant parent > child prev + next prev ~ siblings 基本 :first :last :not(selector) :even :odd :eq(index) :gt(index) :lt(index) :header :animated :focus1.6+ 内容 :contains(text) :empty :has(selector) :parent 可见性 :hidden :visible 属性 [attribute] [attribute=value] [attribute!=value] [attribute^=value] [attribute$=value] [attribute*=value] [attrSel1][attrSel2][attrSelN] 子元素 :nth-child :first-child :last-child :only-child 表单 :input :text :password :radio :checkbox :submit :image :reset :button :file :hidden 表单对象属性 :enabled :disabled :checked :selected 文档处理 内部插入 append(content|fn) appendTo(content) prepend(content|fn) prependTo(content) 外部插入 after(content|fn) before(content|fn) insertAfter(content) insertBefore(content) 包裹 wrap(html|ele|fn) unwrap() wrapall(html|ele) wrapInner(html|ele|fn) 替换 replaceWith(content|fn) replaceAll(selector) 删除 empty() remove([expr]) detach([expr]) 复制 clone([Even[,deepEven]]) 筛选 过滤 eq(index|-index) first() last() hasClass(class) filter(expr|obj|ele|fn) is(expr|obj|ele|fn)1.6* map(callback) has(expr|ele) not(expr|ele|fn) slice(start,[end]) 查找 children([expr]) closest(expr,[con]|obj|ele)1.6* find(expr|obj|ele)1.6* next([expr]) nextall([expr]) nextUntil([exp|ele][,fil])1.6* offsetParent() parent([expr]) parents([expr])

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值