javascript - 您可以从选定的 div 中获取信息吗?

标签 javascript html

编辑

按钮的嵌套方式如下:

该按钮由

触发
$(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/

相关文章:

javascript - 测试 JavaScript 中是否未定义某些内容

javascript - 从 Firestore 读取不会返回任何内容

javascript - APNG num_plays 在 Firefox 中有效,但在 Chrome 中无效

javascript - 打开或关闭分组

JavaScript:找不到元素

html - 标志的链接只有部分功能

html - 绝对 div 中绝对元素的 z-index 问题

javascript - 带有 ng-click 的按钮未触发

Javascript检测表格单元格边框?

html - 水平对齐 DIV 容器内的文本