取得DOM元素節點
Last updated
Last updated
<head>
<script type="text/javascript">
//alert元素id是title物件之標籤內的內容
alert(document.getElementById("title").innerHTML)
</script>
</head>
<body>
<h1 id="title"> 我是Title </h1>
</body><head>
<script type="text/javascript">
//alert元素標籤是h1的內容
alert(document.getElementTagName("h1").innerHTML)
</script>
</head>
<body>
<h1 id="title"> 我是Title </h1>
</body><head>
<script type="text/javascript">
//alert元素name是myInput的值
alert(document.getElementName("myInput").value)
</script>
</head>
<body>
<input name="myInput" type="text" value="大家好" />
</body><head>
<script type="text/javascript">
//alert所有元素class是box的第一個物件之標籤內的內容
alert(document.getElementsByClassName("box")[0].innerHTML)
</script>
</head>
<body>
<div class="box">box1</div>
<div class="box">box2</div>
<div class="box">box3</div>
</body><head>
<script type="text/javascript">
//alert元素class是box的第一個物件之標籤內的內容
alert(document.querySelector(".box").innerHTML)
//元素class是box的所有物件集合(NodeList)
document.querySelectorAll(".box")
</script>
</head>
<body>
<div class="box">box1</div>
<div class="box">box2</div>
<div class="box">box3</div>
</body>