问题
What's the easiest way to get all input fields inside a div without using a javascript library like jQuery? Similar to this jQuery snippet:
var inputs = $('#mydiv :input');
回答1:
document.getElementById('mydiv').getElementsByTagName('input')
回答2:
Try:
var inputs = document.getElementById('mydiv').getElementsByTagName('input');
回答3:
querySelector
and querySelectorAll
will fetch the details of what you're expecting easily.
var divElem = document.getElementById("myDiv");
var inputElements = divElem.querySelectorAll("input, select, checkbox, textarea");
It will give all the input, select, textarea elements in array format.
回答4:
getElementsByTagName
回答5:
document.getElementById("mydiv").getElementsByTagName("input");
回答6:
If you are on modern browsers (ie9+) you can take advantage of querySelectorAll.
var inputs = document.querySelectorAll('#myDiv input');
来源:https://stackoverflow.com/questions/3586305/get-all-input-fields-inside-div-without-js-library