繁体   English   中英

如何获得div元素的子元素

[英]how to get child of div element

<div class="div" id="div">
    <div class="row">
        <div class="col1">
             <input type="text">
             <!--I need to get a value of these inputs-->
        </div>
        <div class="col2">
             <input type="text">
             <!--I need to get a value of these inputs-->
        </div>
    </div>
    <div class="row">
        <div class="col1">
             <input type="text">
             <!--I need to get a value of these inputs-->
        </div>
        <div class="col2">
             <input type="text">
             <!--I need to get a value of these inputs-->
        </div>
    </div>
</div>

如何获取div中所有输入的值以及如何在in(col1或col2)中检查哪个div输入;

在我的情况下,类“行”的div将通过firebase数据库添加。

const inputs = document.querySelectorAll(".div > .col");
alert(inputs.value);

使用JavaScript,尝试使用forEach循环

var inputElem = document.querySelectorAll(".row input");
inputElem.forEach(function(obj){
    console.log(obj.value)
})

片段:

 var inputElem = document.querySelectorAll(".row input"); inputElem.forEach(function(obj){ console.log(obj.value) }) 
 <div class="div" id="div"> <div class="row"> <div class="col1"> <input type="text" value="one"> <!--I need to get a value of these inputs--> </div> <div class="col2"> <input type="text" value="two"> <!--I need to get a value of these inputs--> </div> </div> <div class="row"> <div class="col1"> <input type="text" value="three"> <!--I need to get a value of these inputs--> </div> <div class="col2"> <input type="text" value="four"> <!--I need to get a value of these inputs--> </div> </div> </div> 

我认为这可能有效:

$("div input").each(function(){
    // Get the value:
    console.log($(this).val());
    // check in which div input in(col1 or col2)
    console.log($(this).closest("div").attr("class"));
});

尝试这个:

const inputs = document.getElementById('div').getElementsByTagName('input');
console.log(inputs[0].value, inputs[1].value);

在此处查看示例: https//jsfiddle.net/xbdd6q13/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM