我正在尝试实现可访问性工具,并且在单击按钮后我设法更改了段落的字体大小,但我尝试更改代码以使其适用于所有段落但它不起作用
<script>
function myFunction()
{
var p =document.getElementsByTagName('p'); // Find the element
p.style.fontSize="1.5em"; // Change the style
}
</script>
<button type="button" style="background: #ccc url(images/small.jpg); padding: 0.3em 1em" onclick="myFunction()"></button>
这是它以前对一个段落的工作方式,但我正在尝试不止一个:
<script>
function myFunction()
{
x=document.getElementById("demo") // Find the element
x.style.fontSize="3.0em"; // Change the style
}
</script>
最佳答案
getElementsByTagName
返回 NodeList ,这就像一个数组,因此您必须遍历它们并将样式应用于每个元素:
function myFunction() {
var arr = document.getElementsByTagName('p');
for (var i = 0; i < arr.length; i++) {
arr[i].style.fontSize = "1.5em";
}
}
关于javascript - 使用按钮使用javascript更改整个网站的字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14799291/