编辑
按钮的嵌套方式如下:
该按钮由
触发$(document).on("click" , ".down-arrow" , function() {
});
<div>
<div>
<div class="this is the div I am after">
<!-- To get this text -->
</div>
<div class="down-arrow" id="10">
<!-- this is the div that has the arrow button -->
<img src="downarrow.png" id="10down" width="100%" height="auto">
<img src="uparrow.png" style="display:none" class="upArrow" id="10up" width="100%" height="auto">
</div>
</div>
所以我回显了行,每行都有一个唯一的标识符。
所选按钮位于 div 内,因此当我单击该按钮时,我无法在包含该按钮的 div 内获取一段文本。
所以我使用了.outerHTML
来显示我想要的id,我如何使用.outerHTML
信息专门选择位于按钮外部的id?
var selectedDiv = document.getElementById(selectedId).outerHTML;
最佳答案
如果您正在使用 jQuery(我会假设您使用的是 jQuery),请尝试以下操作:
$('.down-arrow').click({
$(this).parent().children('.targetClass').text();
// where targetClass is the class of whatever it is you want the text of
});
如果您想进一步了解 DOM,请添加更多 .parent()
。
关于javascript - 您可以从选定的 div 中获取信息吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31360485/