简体   繁体   中英

Javascript: click event handler not working

 function check() { var para = document.getElementsByTagName("p"); para[0].style.fontSize = 20; }
 <p>This a paragraph</p> <p>This a paragraph</p> <p>This a paragraph</p> <p>This a paragraph</p> <button onClick="check()">Check</button>

please check the script section I am not able to find any error please help me this

Specify font size in pixels inside string instead of using number 20. Also move the script tag to bottom of body tag.

Please find the working snippit below:

 function check() { var para=document.getElementsByTagName("p"); para[0].style.fontSize= '20px' ; }
 <p>This a paragraph</p> <p>This a paragraph</p> <p>This a paragraph</p> <p>This a paragraph</p> <button onclick="check()">Check</button>

For each Tag P must use like this...

js

function check() {
  var s=document.getElementsByTagName('p');
  for(i=0;i<s.length;i++)
  {
    s[i].setAttribute("style","font-size:20px");
  }
}

View

<p>This a paragraph</p>
<p>This a paragraph</p>
<p>This a paragraph</p>
<p>This a paragraph</p>
<button onClick="check()">Check</button>

you have to define the size in string with px,em,% behind and wrap it with quote

para[0].style.fontSize = 20;

to

para[0].style.fontSize = "20px";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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