简体   繁体   English

如何使用javascript使用onclick在div中显示特定字体类型?

[英]How to display specific type of font inside a div with onclick using javascript?

I have a form with radio buttons each of which representing a different font. 我有一个带有单选按钮的表单,每个按钮代表一种不同的字体。 When the user selects one and hits create the font should appear in the display div. 当用户选择一个并点击创建时,字体应显示在显示div中。

Here is part of HTML and one of the buttons on the form: 这是HTML的一部分,也是表单上的按钮之一:

<div id="form">
    <form id="nameForm" method="get" action="nowhere.php">
        <div class="formbox">
            <div id="radioTitle">
                Pick a font:
            </div>
            <label for="normal" class="normal">Normal</label>    
            <input type="radio" name="fonts" id="normal" />
    </form>
</div>

<div id="display">  
</div>

I'm not sure how to grab the font and get the specific one to display in the div using javascript. 我不确定如何获取字体并使用javascript将其显示在div中。

function fonts(){
    if(wholeForm.normal.checked == true){

    }
}

CSS: CSS:

.new-font {
  font-family: ...;
}

JS: JS:

wholeForm.className = wholeForm.className + " otherclass";

Check out this example: 看看这个例子:

http://jsfiddle.net/ug6Lu/3/ http://jsfiddle.net/ug6Lu/3/

Note that you need any selected font to either be installed on the user's computer or loaded using css/javascript. 请注意,您需要将任何选定的字体安装在用户计算机上或使用css / javascript加载。

HTML: HTML:

<div id="form">
    <form id="nameForm" method="get" action="nowhere.php">
        <div class="formbox">
            <div id="radioTitle">
                Pick a font:
            </div>
            <label for="normal" class="normal">Normal</label>    
            <input type="radio" name="fonts" id="normal" />
    </form>
</div>

<div id="display">  
Example Text
 </div>

JS: JS:

var checkRadio = document.getElementById('normal');
checkRadio.addEventListener("click", modifyFont, false);

function modifyFont(e){
    if(e.target.checked == true) {
        document.getElementById('display').style.fontFamily = "monospace";
    }
}

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

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