【问题标题】:Javascript innerHTML Returns an errorJavascript innerHTML 返回错误
【发布时间】:2014-06-21 15:54:07
【问题描述】:

基本上是我的代码:

  <html>
  <head>
  <script>
  function promptSignIn(){
   var promptbox = document.getElementById('promptbox');

   var xml = '<ul style="list-style-type:none;margin-left:-25px;">
  <li style="font-family:sans-serif;font-size:15;">You must Sign in to access great things...</li>
  <li><br></li>
  <li style="font-family:sans-serif;">Enter Your Email Address:<br><div style="width:280px;height:45px;-webkit-linear-gradient(#e4e4e4, #e9e9e9);
  background: -o-linear-gradient(##e4e4e4, #e9e9e9);
  background: -moz-linear-gradient(#e4e4e4, #e9e9e9);
  background: linear-gradient(#e4e4e4, #e9e9e9);border:1px solid lightgrey;margin-left:-10px;"><input style="-webkit-linear-gradient(#e4e4e4, #e9e9e9);
  background: -o-linear-gradient(##e4e4e4, #e9e9e9);
  background: -moz-linear-gradient(#e4e4e4, #e9e9e9);
  background: linear-gradient(#e4e4e4, #e9e9e9);border:0px;width:280;height:45px;" type="text" value="" name="username"/> </div>
  <br>

  <li style="font-family:sans-serif;">Enter Your Password:<br><div style="width:280px;height:45px;-webkit-linear-gradient(#e4e4e4, #e9e9e9);
  background: -o-linear-gradient(##e4e4e4, #e9e9e9);
  background: -moz-linear-gradient(#e4e4e4, #e9e9e9);
  background: linear-gradient(#e4e4e4, #e9e9e9);border:1px solid lightgrey;margin-left:-10px;"><input style="-webkit-linear-gradient(#e4e4e4, #e9e9e9);
  background: -o-linear-gradient(##e4e4e4, #e9e9e9);
  background: -moz-linear-gradient(#e4e4e4, #e9e9e9);
  background: linear-gradient(#e4e4e4, #e9e9e9);border:0px;width:280;height:45px;" type="password" value="" name="password"/> </div>
  <br>
  <li><input type="submit" value="Sign in" style="margin-left:-10px;width:280px;height:40px;border:0px;background: -webkit-linear-gradient(#2B65EC, #2B60DE);
  background: -o-linear-gradient(#2B65EC, #2B60DE);
  background: -moz-linear-gradient(#2B65EC, #2B60DE);
  background: linear-gradient(#2B65EC, #2B60DE);color:#fff;"/>
  </ul>
  </form>';


   if(promptbox.style.visibility == "hidden"){
    promptbox.style.visibility = "visible";
    promptbox.innerHTML = xml;
   }else{
    promptbox.style.visibility = "hidden";
    promptbox.innerHTML = "";
   }
  }
  </script>
  <title>Droid - Social Networking Platform</title>
  <meta name="description" content="Welcome to Droid, Login or signup to continue">
  </head>
  <body>
  <style>
  body{
  margin:0px;
  }
    #navbar{
    margin-top:0px;margin-right:0px;margin-left:0px;height:125px;
    background: -webkit-linear-gradient(#e1e1e1, #e6e6e6);
  background: -o-linear-gradient(#e1e1e1, #e6e6e6);
  background: -moz-linear-gradient(#e1e1e1, #e6e6e6);
  background: linear-gradient(#e1e1e1, #e6e6e6);
  border-bottom:2px solid #306EFF
    }

  </style>
  <div id="navbar">
  <a href="index">
        <img src="image;/logo.png" width="200" height="85" style="margin:8px;">
  </a>

  <div id="searchbg" style="margin-top:-75px;margin-left:245px;width:440px;height:45px;background: -webkit-linear-gradient(#e4e4e4, #e9e9e9);
  background: -o-linear-gradient(##e4e4e4, #e9e9e9);
  background: -moz-linear-gradient(#e4e4e4, #e9e9e9);
  background: linear-gradient(#e4e4e4, #e9e9e9);border:1px solid grey;border-radius:4px;">
    <form method="get" action="search.php" autocomplete="off">
  <input type="text" style="margin-left:5px;background: -webkit-linear-gradient(#e4e4e4, #e9e9e9);
  background: -o-linear-gradient(##e4e4e4, #e9e9e9);
  background: -moz-linear-gradient(#e4e4e4, #e9e9e9);
  background: linear-gradient(#e4e4e4, #e9e9e9);width:430px;height:45px;border:0px;" value="Search" name="query">

 <input type="submit" value="Search" style="margin:-45px;margin-left:380px;width:60px;height:45px;border-top-right-radius:4px;border-bottom-right-radius:4px;border:0px;background: -webkit-linear-gradient(#2B65EC, #2B60DE);
  background: -o-linear-gradient(#2B65EC, #2B60DE);
  background: -moz-linear-gradient(#2B65EC, #2B60DE);
  background: linear-gradient(#2B65EC, #2B60DE);color:#fff;">
      </form></div>

  <button style="margin:-45px;margin-left:800px;border-radius:3px;width:60px;height:45px;border:0px;background: -webkit-linear-gradient(#2B65EC, #2B60DE);
  background: -o-linear-gradient(#2B65EC, #2B60DE);
  background: -moz-linear-gradient(#2B65EC, #2B60DE);
  background: linear-gradient(#2B65EC, #2B60DE);color:#fff;" onclick="promptSignIn()">Sign in</button>


  </div>

  <div id="promptbox" style="margin: auto; margin-top: 130px; width:300px; height:400px;background: -webkit-linear-gradient(#e1e1e1, #e6e6e6);
  background: -o-linear-gradient(#e1e1e1, #e6e6e6);
  background: -moz-linear-gradient(#e1e1e1, #e6e6e6);
  background: linear-gradient(#e1e1e1, #e6e6e6);border-radius:2px;border:1px solid grey;">
  <div id="prompttitle" style="width:100%;height:40px;margin:0px;border-bottom:1px solid lightgrey;">
  <h1 id="promptTitle" style="margin:3px;font-family:sans-serif;font-size:15;">Please Sign in</h1>
  <img src="image;/close.png" width="20" height="20" style="margin-top:-25px;margin-left:280px;" onclick="closePrompt()"/>
  </div>
  <div id="promptContent" style="margin-top:45px;margin:auto;width:290px;height:350px;">

 </div>

  </div>

  </body>
  </html>

当用户想要登录时,我希望他们看到里面有登录的提示框,这个 1 框将用于所有提示,所以我需要它每次更改内部 html 代码单击不同的按钮。任何建议,我是 Javascript 的初学者。

【问题讨论】:

  • 你的内联 css 真的很难阅读......
  • Javascript 中不能有换行符,但为什么所有的内联 CSS 也一样?
  • 我已经习惯这样编程了,哈哈
  • 这不是很聪明。将所有 html 放在正文中并将提示框设置为 display:none - 只需将所有内容都移到 promptContent 中,这就是您可能想要的

标签: javascript onclick innerhtml


【解决方案1】:

Live Demo

  1. 将 CSS 更改为不内联
  2. 将 XML var 中的内容移动到 promptContent 中
  3. 将脚本改为

window.onload = function () {
  document.getElementById('promptbox').style.display = "none";
  document.getElementById('signin').onclick = function () {
    var promptbox = document.getElementById('promptbox');
    promptbox.style.display = promptbox.style.display == "none" ? "block" : "none"
  }
  document.getElementById('closePrompt').onclick = function () {
    document.getElementById('promptbox').style.display = "none";
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 2017-11-10
    • 2013-10-06
    • 2019-03-30
    相关资源
    最近更新 更多