简体   繁体   English

IE中的JavaScript字体家族问题

[英]JavaScript Font-Family issue in IE

Can someone think of a reason that this wouldn't work in any version of IE? 有人能想到在任何版本的IE中都无法使用的原因吗? I have a drop down select menu for choosing the font family of an element, which calls a javascript function to change the font-family. 我有一个下拉选择菜单,用于选择元素的字体系列,该菜单调用javascript函数来更改字体系列。 Here is the html... 这是HTML ...

 <select id="selecth1FontFamily" name="selectFontFamily" onchange="updateh1family();">
                                  <option> Serif </option>
                                  <option> Arial </option>
                                  <option> Sans-Serif </option>                                  
                                  <option> Tahoma </option>
                                  <option> Verdana </option>
                                  <option> Lucida Sans Unicode </option>                               
                              </select>

And here is the JavaScript... 这是JavaScript ...

function updateh1family() {

        var selector = document.getElementById('selecth1FontFamily');
        var cssPreviewSpan = document.getElementById('h1FontFamily');

        cssPreviewSpan.innerHTML = selector.value;
        // also update the CSS preview

        var h1 = document.getElementById('liveh1')
        h1.style.fontFamily =  selector.value;
    }

This works to change the font family of the element in EVERY browser, minus the dreaded internet explorer. 这可以更改每个浏览器中元素的字体系列,减去可怕的Internet Explorer。 Any thoughts? 有什么想法吗? I mean, it is a fairly straightforward function, I tried to think of other ways to go about it but I'm pretty much stuck. 我的意思是,这是一个相当简单的功能,我尝试过考虑其他解决方法,但是我几乎陷入了困境。 Thank you to all who read this! 感谢所有阅读本文的人!

If you debugged the code you would see that selector.value returns nothing. 如果您调试了代码,您将看到选择器。值不返回任何内容。

<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <h1 id="liveh1">Some text</h1>
  <select id="selecth1FontFamily" name="selectFontFamily" onchange="updateh1family();">
    <option> Serif </option>
    <option> Arial </option>
    <option> Sans-Serif </option>                                  
    <option> Tahoma </option>
    <option> Verdana </option>
    <option> Lucida Sans Unicode </option>                               
  </select>
    <script>
      function updateh1family() {
        var selector = document.getElementById('selecth1FontFamily');
        var family = selector.options[selector.selectedIndex].value;
        var h1 = document.getElementById('liveh1')
        h1.style.fontFamily = family;        
      }

    </script>
</body>
</html>

JSBin 联合会

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

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