简体   繁体   English

Javascript:单击事件处理程序不起作用

[英]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.在字符串内以像素为单位指定字体大小,而不是使用数字 20。同时将脚本标签移动到正文标签的底部。

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...对于每个标签 P必须像这样使用......

js 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你必须用 px,em,% 后面定义字符串的大小并用引号包裹它

para[0].style.fontSize = 20;

to

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

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

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