简体   繁体   English

尝试使用Javascript调整点击文字的大小,我在做什么错?

[英]Trying to resize text on click with Javascript, what am I doing wrong?

I'm trying and failing to create an on click function to resize some text. 我正在尝试并且无法创建点击功能来调整某些文本的大小。 Can anyone see what I'm doing wrong? 谁能看到我在做什么错? I definitely know the code isn't correct, because I'm such a noob at Javascript. 我绝对知道代码是不正确的,因为我对Javascript非常陌生。 Cheers in advance! 提前加油!

    <div class="Appraisals" id="change">
  <div align="left"><span class="Block1">Random text that nobody cares about.
Random text that nobody cares about.Random text that nobody cares about. </span></div> 
    <button type="button" 
onclick="document.getElementById('change').style.height "50px"; >
Click Me!</button>
</div>
<div class="inspire"> <img src="inspire.jpg" alt="Crest" height="200"></div>

it should be 它应该是

document.getElementById('change').style.fontSize = '50px'

instead of: 代替:

document.getElementById('change').style.height "50px";

FIDDLE 小提琴

Option one: 选项一:

Call myFunction onclick and pass the ID and the font-size, them handle with the manipulation in the script file. 调用myFunction onclick并传递ID和字体大小,它们通过脚本文件中的操作进行处理。 (good) (好)

HTML: HTML:

    <div class="Appraisals" id="change">
      <div align="left"><span class="Block1">Random text that nobody cares about.
         Random text that nobody cares about.Random text that nobody cares about.</span>
      </div> 
      <button type="button" onclick="myFunction('change', '50px');" > Click Me!</button>
       Click Me!
      </button>
    </div>
    <div class="inspire"> <img src="inspire.jpg" alt="Crest" height="200"></div>

JS: JS:

<script>
   function myFunction(element, clr) {
     var changeId = document.getElementById(element);
     changeId.style.fontSize=clr;
   }
</script>

Option two: 选项二:

handle with the manipulation in the HTML. 处理HTML中的操作。 (not good) (不好)

    <div class="Appraisals" id="change">
      <div align="left"><span class="Block1">Random text that nobody cares about.
         Random text that nobody cares about.Random text that nobody cares about.</span>
      </div> 
      <button type="button" onclick="document.getElementById("change").style.fontSize="50px"" > Click Me!</button>
       Click Me!
      </button>
    </div>
    <div class="inspire"> <img src="inspire.jpg" alt="Crest" height="200"></div>

Option three: 选项三:

Use jquery. 使用jQuery。 (very good) (很好)

First call jquery cdn: 首先调用jquery cdn:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

Them: 他们:

<script>
  $(document).ready(function() {
     $('button').click(function() {
       $('#change').css('font-size', '50px');
     });
  });
</script>

是否要更改文本的大小而不是高度?

document.getElementById('change').style = 'font-size:50px;'

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

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