简体   繁体   English

javascript onclick函数以放大文本

[英]javascript onclick function to enlarge text

I'm not sure why, but I can't seem to get this to work. 我不确定为什么,但似乎无法使它正常工作。

Here is my function to enlarge my font. 这是我放大字体的功能。

<script type="text/javascript">
    function growText() {
        var text = document.getElementById("t_left_text");
        text.font-size=22px;        
</script>

And here is where I call it 这就是我所说的

<div id="t_left" onclick="growText()">
    <br />
    <p id="t_left_text">Mountains are beautiful land structures <br /> that are a result of plate tectonics.</p>
    <br />
</div>

Try: 尝试:

text.style.fontSize = "22px";

DEMO: http://jsfiddle.net/C2MWN/ 演示: http : //jsfiddle.net/C2MWN/

When you want to change an element's CSS, you need to use the style property. 当您想更改元素的CSS时,需要使用style属性。 To determine the name of the specific style property, the CSS name is converted to camel case - "font-size" becomes "fontSize", so that the identifier is valid in JavaScript. 为了确定特定样式属性的名称,将CSS名称转换为驼峰大小写-“ font-size”变为“ fontSize”,以便标识符在JavaScript中有效。

While setting the style properties definitely works, and although this is a very simple example, it might be easier to deal with adding and removing a class . 虽然设置style属性绝对可以,并且尽管这是一个非常简单的示例,但添加和删除class可能更容易。 This is especially useful when setting multiple CSS properties. 在设置多个CSS属性时,这尤其有用。 The class could be defined as: 该类可以定义为:

.enlarged-font {
    font-size: 22px;
}

And you would manipulate the text.className property (and/or the classList property). 然后您将操纵text.className属性(和/或classList属性)。

Depending on the browser you're using, you could have easily provided a better description (as obvious as it was for some of us) of the problem by using the JavaScript console in the browser. 通过使用浏览器中的JavaScript控制台,可以轻松地对问题进行更好的描述(对我们中某些人来说是显而易见的),具体取决于所使用的浏览器。 In Firefox, you could use Firebug. 在Firefox中,您可以使用Firebug。 In Internet Explorer and Chrome, you could use Developer Tools. 在Internet Explorer和Chrome中,您可以使用开发人员工具。 If installed/enabled, these can usually be brought up by pressing the F12 on your keyboard. 如果已安装/启用,则通常可以通过按键盘上的F12来调出它们。

Also, don't forget to close your function with a } . 另外,不要忘记使用}关闭函数。

Reference: 参考:

Use below code 使用以下代码

function growText() {
            var text = document.getElementById("t_left_text");
            text.style.fontSize ="22px";
}

Working example http://jsfiddle.net/D2anZ/ 工作示例http://jsfiddle.net/D2anZ/

Here's a version that uses CSS to accomplish what you want. 这是使用CSS完成您想要的版本。 That way if you want to do this to different sets of text at the same time, and want to change that font size, there's only one place you need to make the change. 这样,如果您想同时对不同的文本集执行此操作,并且想要更改该字体大小,则只需要在一个位置进行更改。 (Or if you also want to add other css properties (color, etc.) (或者,如果您还想添加其他CSS属性(颜色等),

Fiddle 小提琴

JavaScript 的JavaScript

function growText() {
    var text = document.getElementById("t_left_text");
    text.className = 'large-font';
}

CSS 的CSS

.large-font {
    font-size: 22px;
}

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

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