我正在尝试在我的网站上选择 3 或 4 种字体样式

我想创建一个选择框来更改字体,并且我想在 3 或 4 种字体样式之间进行选择


前任。当我点击字体 1 时,对于所有网站来说,它必须更改为“Franklin Gothic Medium”,这才是重点。


    <script>

function changeFont() {

  document.getElementById("body").style.fontFamily = 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;

}

    <script>


    <div class="option-box">

    <h4>Font Style</h4>

    <div class="fonts">

     <select name="" id="myfont">

       <option>font 1</option>

       <option>font 2</option>

       <option>font 3</option>

    </select>

     </div>

     </div>


缥缈止盈
浏览 91回答 2
2回答

潇潇雨雨

一步步学习或者花钱找人来做就可以了。这是一个开始,学习倾听select变化。<html>&nbsp; <body>&nbsp; &nbsp; <select id="myfont">&nbsp; &nbsp; &nbsp; <option value='robotFont'>font 1</option>&nbsp; &nbsp; &nbsp; <option value='secondFont'>font 2</option>&nbsp; &nbsp; &nbsp; <option value='uglyFont'>font 3</option>&nbsp; &nbsp; </select>&nbsp; <script>&nbsp; &nbsp; const fontSelector = document.getElementById('myfont');&nbsp; &nbsp; fontSelector.addEventListener('change', () => {&nbsp; &nbsp; &nbsp; console.log("fontSelector change", fontSelector.value);&nbsp; &nbsp; });&nbsp; </script>&nbsp; </body></html>下一步可能是有一些 css 类来指定字体,例如.robotFont,...修改eventListener来切换body上的字体类

12345678_0001

仅基本CSS解决方案(JS是注入CSS而不需要滚动其中一个框)document.querySelector( 'style' ).innerHTML += `&nbsp; html {&nbsp; &nbsp; color: #222;&nbsp; &nbsp; font-family: Arial;&nbsp; &nbsp; text-transform: capitalize;&nbsp; }&nbsp; html, body, h2, p, ul {&nbsp; &nbsp; margin: 0;&nbsp; &nbsp; padding: 0;&nbsp; &nbsp; list-style-type: none;&nbsp; }&nbsp; body {&nbsp; &nbsp; padding: 1rem;&nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; h2 {&nbsp; &nbsp; margin-bottom: 0.5rem;&nbsp; }&nbsp; main .button {&nbsp; &nbsp; cursor: pointer;&nbsp; }&nbsp; .button {&nbsp; &nbsp; position: relative;&nbsp; }&nbsp; .dropdown {&nbsp; &nbsp; display: none;&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; top: 2.75rem;&nbsp; &nbsp; left: -0.1rem;&nbsp; &nbsp; right: -0.1rem;&nbsp; &nbsp; background-color: #fff;&nbsp; }&nbsp; #toggle:checked ~ main .dropdown { display: block; }`;main .button, span, span::after, .dropdown, li {&nbsp; display: inline-block;&nbsp; padding: 0.25rem;&nbsp; border-style: solid;&nbsp; border-color: #666;&nbsp; user-select: none;}.button:hover, .button:hover .span_wrapper span::after,li:hover {&nbsp; background-color: #eee;}.button:active, .button:active .span_wrapper span::after, li:active {&nbsp; background-color: #ccc;}main p {&nbsp; padding: 0.25rem 1rem;}.span_wrapper {&nbsp; transform: scale( 0.75 ) translateY( -0.125rem ); border-style: none;}.span_wrapper span, .span_wrapper span::after {&nbsp; transform: rotate( 45deg ); width: 1rem; height: 1rem;&nbsp; padding: 0; border-style: none;&nbsp; background-color: #222; content: "";}.span_wrapper span::after {&nbsp; transform: translateY( -0.125rem ) translateX( -0.125rem );&nbsp; background-color: #fff;}#arial:checked ~ main { font-family: Arial; }#verdana:checked ~ main { font-family: Verdana; }#georgia:checked ~ main { font-family: Georgia; }input { display: none; }html body { padding-top: 0 }<style>&nbsp; li {&nbsp; &nbsp; display: block; padding: 0.5rem; border-style: none;&nbsp; }</style><header></header><input type="checkbox" id="toggle"><input type="radio" id="arial" name="font"><input type="radio" id="verdana" name="font"><input type="radio" id="georgia" name="font"><main>&nbsp; <h2>font style</h2>&nbsp; <label for="toggle">&nbsp; &nbsp; <div class="button">&nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; arial&nbsp; &nbsp; &nbsp; &nbsp; <span class="span_wrapper">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span></span>&nbsp; &nbsp; &nbsp; &nbsp; </span>&nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; <div class="dropdown">&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><label for="arial">Arial</label></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><label for="verdana">Verdana</label></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><label for="georgia">Georgia</label></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </div>&nbsp; </label>&nbsp; <br><br>&nbsp; <p>lorem ipsum</p></main>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript