一. getdocumentid
getdocumentid用于通过id属性获取HTML元素。
<!DOCTYPE html>
<html>
<head>
<script>
function getdocumentid() {
var element = document.getElementById("example");
element.style.color = "red";
}
</script>
</head>
<body>
<p id="example">Hello World!</p>
<button onclick="getdocumentid()">Click me</button>
</body>
</html>
以上示例中,定义了一个getdocumentid函数,在点击按钮时该函数会获取id为example的元素,并将其字体颜色改为红色。
二. getdocument函数
getdocument函数用于通过选择器获取HTML元素。
<!DOCTYPE html>
<html>
<head>
<script>
function getdocument() {
var element = document.querySelector(".example");
element.style.color = "blue";
}
</script>
</head>
<body>
<p class="example">Hello World!</p>
<button onclick="getdocument()">Click me</button>
</body>
</html>
以上示例中,定义了一个getdocument函数,在点击按钮时该函数会获取class为example的元素,并将其字体颜色改为蓝色。
三. getdocumentbyunid
getdocumentbyunid用于通过唯一标识符(unid)获取HTML元素。
<!DOCTYPE html>
<html>
<head>
<script>
function getdocumentbyunid() {
var element = document.getElementsByName("example")[0];
element.style.color = "green";
}
</script>
</head>
<body>
<p name="example">Hello World!</p>
<button onclick="getdocumentbyunid()">Click me</button>
</body>
</html>
以上示例中,定义了一个getdocumentbyunid函数,在点击按钮时该函数会获取name为example的元素,并将其字体颜色改为绿色。
四. getdocumentbyclass
getdocumentbyclass用于通过class属性获取HTML元素。
<!DOCTYPE html>
<html>
<head>
<script>
function getdocumentbyclass() {
var elements = document.getElementsByClassName("example");
for (var i = 0; i < elements.length; i++) {
elements[i].style.color = "purple";
}
}
</script>
</head>
<body>
<p class="example">Hello World!</p>
<p class="example">Hello World!</p>
<p class="example">Hello World!</p>
<button onclick="getdocumentbyclass()">Click me</button>
</body>
</html>
以上示例中,定义了一个getdocumentbyclass函数,在点击按钮时该函数会获取所有class为example的元素,并将它们的字体颜色改为紫色。
五. getdocumentelement结果为null
getdocumentelement用于获取当前元素的父元素。
<!DOCTYPE html>
<html>
<head>
<script>
function getdocumentelement() {
var element = document.getElementById("example");
var parent = element.parentElement;
if (parent != null) {
parent.style.color = "orange";
}
}
</script>
</head>
<body>
<div>
<p id="example">Hello World!</p>
</div>
<button onclick="getdocumentelement()">Click me</button>
</body>
</html>
以上示例中,定义了一个getdocumentelement函数,在点击按钮时该函数会获取id为example的元素的父元素,并将其字体颜色改为橙色。但是,由于该元素没有父元素,因此可能会返回null值。
六. getdocumentbyid vue
getdocumentbyid用于通过id属性获取HTML元素。
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
function getdocumentbyid() {
var element = document.getElementById("example");
element.style.color = "red";
}
</script>
</head>
<body>
<div id="app">
<p id="example">Hello World!</p>
<button @click="getdocumentbyid">Click me</button>
</div>
</body>
</html>
<script>
var app = new Vue({
el: '#app',
methods: {
getdocumentbyid: getdocumentbyid
}
})
</script>
以上示例中,定义了一个getdocumentbyid函数,在点击按钮时该函数会获取id为example的元素,并将其字体颜色改为红色。该示例使用了Vue框架,将其与方法getdocumentbyid绑定到按钮上,在点击按钮时触发方法事件。
七. getdocumentbyid
getdocumentbyid用于通过id属性获取HTML元素。
<!DOCTYPE html>
<html>
<head>
<script>
function getdocumentbyid() {
var element = document.getElementById("example");
element.style.color = "red";
}
</script>
</head>
<body>
<p id="example">Hello World!</p>
<button onclick="getdocumentbyid()">Click me</button>
</body>
</html>
以上示例中,定义了一个getdocumentbyid函数,在点击按钮时该函数会获取id为example的元素,并将其字体颜色改为红色。
八. getdocumentbase
getdocumentbase用于获取当前文档的base元素。
<!DOCTYPE html>
<html>
<head>
<base href="https://www.example.com/path/" target="_blank">
</head>
<body>
<script>
var base = document.baseURI;
document.write(base);
</script>
</body>
</html>
以上示例中,获取了文档中的base元素并输出了其href属性。baseURI属性返回当前文档的URI。
原创文章,作者:EGAI,如若转载,请注明出处:https://www.506064.com/n/138515.html
微信扫一扫
支付宝扫一扫