简体   繁体   English

JavaScript onclick未显示在页面上

[英]JavaScript onclick not displaying on page

I want to display a form after clicking on a link. 我想在单击链接后显示一个表单。 ON the same page. 在同一页上。 Why is this code not working ? 为什么此代码不起作用?

<a href="javascript:;" onclick="forgot();" class="txtmenu">Click Here if you forgot your password</a>

<script type='text/javascript'>
function forgot()
{
  document.write("

<form action=\"<?php echo $_SERVER['PHP_SELF']?>\" method=\"post\" > 
<table width=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">
  <tr>
    <td>Enter your email</td>
  </tr>
   <tr>
    <td><input type=\"text\" name=\"username\" maxlength=\"40\" class=\"form-field\" ><input class=\"submit-button\" type=\"submit\" name=\"forgot\" value=\"SEND MY PASS\" /></td>
  </tr>
</table>
</form>

 ");  
}
</script>

You must add an \\ symbol at all the end of lines in multiline string. 您必须在多行字符串的所有行尾添加一个\\符号。

Like: 喜欢:

function forgot()
{
document.write("\
<form action=\"<?php echo $_SERVER['PHP_SELF']?>\" method=\"post\" >\
<table width=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\
  <tr>\
    <td>Enter your email</td>\
  </tr>\
   <tr>\
    <td><input type=\"text\" name=\"username\" maxlength=\"40\" class=\"form-field\" ><input class=\"submit-button\" type=\"submit\" name=\"forgot\" value=\"SEND MY PASS\" /></td>\
  </tr>\
</table>\
</form>\
 ");  
}

It doesnt work because you can't split a string over multiple lines like that. 这是行不通的,因为您不能将字符串分割成多行。 While I wouldnt recommend doing it your way with the document.write, you can fix it by doing this: 虽然我不建议您以document.write的方式进行操作,但是您可以通过执行以下操作来修复它:

function forgot()
{
  document.write(

"<form action=\"<?php echo $_SERVER['PHP_SELF']?>\" method=\"post\" > " +
"<table width=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"" +
      "align=\"center\">" +
  "<tr>"+
    "<td>Enter your email</td>"+
  "</tr>"+
   "<tr>"+
    "<td><input type=\"text\" name=\"username\" maxlength=\"40\""+ "class=\"form-field\" ><input class=\"submit-button\" type=\"submit\""+ "name=\"forgot\" value=\"SEND MY PASS\" /></td>"+
  "</tr></table></form>"

 );  
}

It's ugly but if you're going to be using document.write then it doesnt really matter as much ;) 这很丑陋,但是如果您要使用document.write,那么它并不重要;)

You should not just document.write after document finished loading. 文档加载完成后,您不应该只是document.write。 It will not make you any good. 这不会给你带来任何好处。 You should have some id for the element and modify it later when some event happens. 您应该为该元素提供一些ID,并在发生某些事件时稍后对其进行修改。

For example you create a div where id="myDIV" and make it invisible and the onclick you just 例如,您创建一个id为“ myDIV”的div并使其不可见,而onclick只是

var itisMyDiv = document.getElementById("myDIV");
itIsMyDiv.style.visibility = "visible";

or something like that. 或类似的东西。

You must add an \\ symbol at all the end of lines in multiline string or use string concatenation (see here ). 您必须在多行字符串的所有行尾添加一个\\符号,或使用字符串串联(请参阅此处 )。

<a href="javascript:;" onclick="forgot()" class="txtmenu">Click Here if you forgot your password</a>
<script type='text/javascript'>
    function forgot()
    {
    document.write("\
    <form action=\"<?php echo $_SERVER['PHP_SELF']?>\" method=\"post\" >\
    <table width=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\
      <tr>\
        <td>Enter your email</td>\
      </tr>\
       <tr>\
        <td><input type=\"text\" name=\"username\" maxlength=\"40\" class=\"form-field\" ><input class=\"submit-button\" type=\"submit\" name=\"forgot\" value=\"SEND MY PASS\" /></td>\
      </tr>\
    </table>\
    </form>\
     ");  
    }
</script>

Using document.write() is bad idea, because it will overwrite the whole document. 使用document.write()是个坏主意,因为它将覆盖整个文档。 A quick and better way is to have some div , assign an id attribute to that div, then in forgot() function retrive that div using document.getElementById and replace contents by assigning your string to innerHTML property. 一种更快更好的方法是设置一些div ,为该div分配一个id属性,然后在forgot()函数中使用document.getElementById检索该div并通过将字符串分配给innerHTML属性来替换内容。

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

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