close
如果我已經取得form元素,要如何取得底下的tag呢?
var x = document.getElementById("myForm").elements.length;
語法:
formObject.elements
屬性:
length
方法:
[index]
item(index)
namedItem(id)
範例:
var x = document.getElementById("myForm").elements[0].value;
var x = document.getElementById("myForm").elements.item(0).value;
var x = document.getElementById("myForm").elements.namedItem("fname").value;
var x = document.getElementById("myForm"); var txt = ""; var i; for (i = 0; i < x.length; i++) { txt = txt + x.elements[i].value + "<br>"; } document.getElementById("demo").innerHTML = txt;
2019/06/03 新增:
2.
document.forms[0]['txtPasswd'].focus(); // 'txtPasswd' 是id名稱.
3.
var inpuForm = document.getElementById("form1").getElementsByClassName("inputField");
參考:https://stackoverflow.com/questions/7815374/get-element-inside-element-by-class-and-id-javascript
取得元素方法總整理:getElementById(), getElementsByTagName(), getElementsByClassName(), getElementsByName(), querySelector(css_selector)
querySelectorAll(css_selector)
var xx = document .querySelectorAll("p");
- div → all div elements.
- div p → all p inside div.
- #y683 → element with id="y683"
- div.xyz → all div has class xyz.
- input[name="pets1"]:checked → all element with name="pets1" and checkbox checked
文章標籤
全站熱搜
留言列表