javascript - 使用按钮使用javascript更改整个网站的字体大小

标签 javascript html fonts web accessibility

我正在尝试实现可访问性工具,并且在单击按钮后我设法更改了段落的字体大小,但我尝试更改代码以使其适用于所有段落但它不起作用

<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/

相关文章:

javascript - 我无法让我的 map 显示在传单 JS 中

javascript - 根据辅助数组中的第一个字符串从最大到最小字符串大小对二维数组进行排序?

ios - 如何从设置中更改应用程序的字体?

html - CSS 下载字体在 Firefox ("Content Blocked Source"中被阻止)

javascript - 无法循环遍历类名 JavaScript

html - 悬停时更改元素的背景颜色 css

javascript - 我想用 JavaScript 交换 2 个相同的 HTML 元素

html - 为什么这个 div 的宽度在 IE7 中横跨整个页面?

windows - 如何为通过 CreateWindow 创建的窗口指定字体?

javascript - 为什么构造函数中需要apply()函数