jquery get neighbor html value
jquery get neighbor html value
我有以下html
<div class="wrapper">
<h5>Testing</h5>
<ul>
<li><span class='ent'>test</span></li>
<li><span class='ent'>test</span></li>
</ul>
</div>
<div class="wrapper">
<h5>Testing123</h5>
<ul>
<li><span class='ent'>test</span></li>
<li><span class='ent'>test</span></li>
</ul>
</div>
我有一个点击功能
$('.ent').on('click', function() {
alert($(this).html()); #This works fine
alert($(this).find('h5').html()) #This is undefined
})
我可以假设这是引用当前元素,但是我如何获取邻居元素html。
尝试
alert($(this).closest('ul').prev('h5').html());
或
alert($(this).parent().prev('h5').html());
或
alert($(this).closest('.wrapper').find('h5').html());
你的代码有问题
.find()
向下看你的元素h5
上面的树,所以你需要使用类wrapper
获取父element
而不是使用find
或获取父ul
并使用.prev()
获取前一个元素参考
.closest()
.parent()
.prev()
.find()
相关文章:
- HTML Value finding
- HTML SELECT-使用JavaScript按VALUE更改所选选项
- 如何将value和id动态添加到html下拉选项标记中
- 为什么 HTML 中的 Value 属性在与 AngularJS 中的 ng-model 一起使用时不起作用
- $.html is not having textarea value
- document.getElementById(foo).value,带有html编码的特殊字符
- limiting only value 0/1 in <td> html
- 从javascript insertCell HTML输入标记传递this.value
- jQuery get HTML value
- jquery get neighbor html value
- javascript HTML get value
- 使用DOM按钮更改Select HTML元素中的Selected Value单击select2.js
- Get an specific value in <a> html with javascript-ajax
- HTML表单,onclick="this.disabled=true;this.value='发送中,
- 重置HTML选择元素中的Type-A-Value特性
- HTML标签的value属性可以用来存储JavaScript代码吗?
- xpath and html value (javascript)
- HTML onchange (this.value)
- 为什么隐藏输入元素的.attr('value')没有从html字符串返回原始值
- 读取HTML表单的innerHTML与VALUE属性(&它的值)输入标签