【发布时间】:2021-12-13 16:18:42
【问题描述】:
我有一个非常基本的代码。它只是在单击时更改按钮的样式。使用getElementById 时效果很好,但我想以XPATH 的方式进行操作。我是 JavaScript 新手。我做错了什么,我该如何做到这一点?
代码:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to change the layout of this paragraph</p>
<button onclick="myFunction()">Click Me!</button>
<script>
function myFunction() {
let x = document.getElementByXpath("//html[1]/body[1]/button[1]");
x.style.fontSize = "25px";
x.style.color = "red";
}
</script>
</body>
</html>
【问题讨论】:
-
没有
document.getElementByXpathdeveloper.mozilla.org/en-US/docs/Web/API/Document/evaluate -
没有 XPATH 有很多更好的方法来做到这一点。
标签: javascript html dom xpath