简体   繁体   English

如何通过JavaScript更改字体大小

[英]How change font-size via javascript

How can I change the font size of the left-column using JavaScript? 如何使用JavaScript更改left-column的字体大小?

<div id="left-column">
    <a href="">1</a><br>
    <a href="">2</a><br>
    <a href="">3</a><br>
    <a href="">4</a><br>
    <a href="">5</a><br>
    <a href="">6</a><br>
</div>

This is what I have so far: 这是我到目前为止的内容:

<script>
    document.write(document.body.clientWidth);
    if (document.body.clientWidth > 400)
         document.getElementById('left-column').style.letterSpacing="0px";              
    }
 </script>

You use the same property name that you would in CSS (minus the hypen and with camel casing) against the style object. 您对style对象使用与CSS中相同的属性名称(减去连字符和带有驼峰的大写字母)。 Now, font-size is an "inherited" property, which means that when it is applied, the value is also applied to descendant elements as well. 现在, font-size是一个“继承”属性,这意味着当应用它时,该值也将应用于后代元素。 So, while can apply this to each <a> element if you like, you don't need to. 因此,虽然可以根据需要将其应用于每个<a>元素,但无需这样做。 You can just apply it to the parent element of them. 您可以将其应用于它们的父元素。 Keep in mind though that ALL descendant elements will be affected in that case and you would have to then reset the font on elements that you didn't want affected. 请记住,尽管在这种情况下所有后代元素都会受到影响,然后您必须在不希望受到影响的元素上重置字体。

 document.querySelector("button").addEventListener("click", function(){ var iw = window.innerWidth; document.getElementById("outputArea").textContent = "The usable width of the page is: " + iw + "px"; if(iw > 400){ // Get all the <a> elements that are children of the div var a = document.querySelectorAll("#leftcol > a"); // loop through the colleciton of them and change their font size for(var i = 0; i < a.length; i++){ a[i].style.fontSize = "3em"; } // We could avoid getting all the <a> elements and looping through them one by one // and just do this: // document.getElementById("leftcol").style.fontSize = "3em"; // And that would affect the the elements within the #leftcol element. } }); 
 <div id="outputArea"></div> <div id="leftcol"> <a href="">1</a><br> <a href="">2</a><br> <a href="">3</a><br> <a href="">4</a><br> <a href="">5</a><br> <a href="">6</a><br> </div> <button>Click Me</button> 

Also, document.write() should not be used unless you are dynamically writing an entire document's DOM (usually into a new window). 此外, document.write()不应该被使用,除非你是动态编写整个文档的DOM(通常到一个新窗口)。 Instead, inject the content you want to output into an element already in the DOM. 而是将要输出的内容注入到DOM中已经存在的元素中。

You should use CSS as oppose to javascript to control your font size based on page width. 您应该将CSS与JavaScript相对使用,以根据页面宽度控制字体大小。 The best way to do that is using media queries. 最好的方法是使用媒体查询。 See this page for more details. 有关更多详细信息,请参见此页面。 https://www.w3schools.com/css/css3_mediaqueries_ex.asp https://www.w3schools.com/css/css3_mediaqueries_ex.asp

Example: 例:

#leftcol{
    font-size: 20px;
}

@media screen and (max-width: 400px) {
    #leftcol {
        font-size: 12px;
    }
}

With the above example, the font size will be 20px by default. 在上面的示例中,默认情况下,字体大小为20px。 When the page is 400 pixels or less, it will be reduced to size of 12. 当页面小于或等于400像素时,页面尺寸将缩小为12。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM