简体   繁体   中英

How to make javascript code to be embed into html button?

I am a noob for learning javascripts

Here I have a javascripts code

I made a button named "click"

when you click the button

it will send a value xx=1 to x

if the value is match with the condition

then it will print "Y", otherwise it will be "N"

but it always cannot shows the messagebox.

<html>
  <head>
  <title>test</title>
  <script language="javascript">
    function jclass(xx)
    {
       var x=xx;
       if x==1
          alert("Y");
       else
          alert("N");                 
    }
  </script>
  </head> 
  <head>
    <h1><a href="javascript:jclass(1);">click</h1>
  </head>
</html>

I have also tried other ways like this.

but it still cannot work.

<html>
  <head>
  <title>test</title>
  <script language="javascript">
    function jclass()
    {
       var x=1;
       if x==1
          alert("Y");
       else
          alert("N");                 
    }
  </script>
  </head> 
  <head>
    <h1><a href="javascript:jclass();">click</h1>
  </head>
</html>

您必须使用括号 if(x == 1)

You are missing the brackets for comparison,its not python:) You can check these type of errors in console and act upon exact statement You should also put curly braces even for a single line in a block or condition to avoid any legacy or formatting issues when running code

Happy Programming

 <html> <head><title>test</title> <script language="javascript"> function jclass(xx) { var x=xx; if (x==1) { alert("Y"); } else { alert("N"); } } </script> </head> <head> <h1><a href="javascript:jclass(1);">click</h1> </head> </html>

You have syntax error in if condition. The basic if...else syntax requires condition inside parenthesis after if .

 function jclass(xx){ var x=xx; if (x==1) alert("Y"); else alert("N"); }
 <h1><a href="javascript:jclass(1);">click</h1>

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