抱歉我的英语不好,例如我有2列,每列当点击div.open-model
时都会显示一个模型,其值为h3.title
,我使用了 jQuery,但我无法获取每列的值 h3.title
$(document).ready(function() {
$('.open_modal').click(function(e) {
var h3title = $(this).find('.parent .title').html();
console.log('h3title');
e.preventDefault();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
<h3 class="title">Title1</h3>
</div>
<div class="open-model">Open model</div>
<div class="parent">
<h3 class="title">Title2</h3>
</div>
<div class="open-model">Open model</div>
最佳答案
尝试prev()
。
var h3title = $(this).prev('.parent').find(".title").text();
修复其他错误后
$(document).ready(function() {
$('.open-model').click(function(e) {
var h3title = $(this).prev('.parent').find(".title").text();
console.log(h3title);
e.preventDefault();
});
});
关于javascript - 单击父元素外部的div时如何获取元素子元素中的值JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45074979/