Javascript/Html ReplaceChild Url btn


代码

可以在以下位置查看:

                 http://fxv4.com/js/java.html

(来源:view-source:http://fxv4.com/js/java.html )

问题是里面的代码不会改变网址。问题不大,但如果删除标签,该功能不起作用。也不是倒计时。我只想让 js btn 中的 url 像普通提交/js btn 一样隐身。

感谢任何帮助,谢谢!

当前的代码:

<a href="http://fxv4.com/js/java.html" id="download" class="button"> 
  <button onclick="window.location='http://URL undefined';">Visit Page Now</button>
</a>

添加到按钮 onclick 返回语句 false,因此 click 事件不会发送到父元素:

<a href="http://fxv4.com/js/java.html" id="download" class="button"> 
  <button onclick="window.location='http://URL undefined';return false">Visit Page Now</button>
</a>

你必须重新排列你的代码并了解 HTML/JS 的基础知识。请记住以下几点:

  1. 你应该只把HTML元素放在<body>里面,不能放在head里面。
  2. 更改 HTML 元素的 JavaScript 代码必须在 DOM 文档加载后执行。

例:

<html>
  <head>
    <script type="text/javascript">
      // (2) Run JavaScript after DOM document has loaded.
      window.onload = function() {
          var message = document.getElementById('message');
          var button = document.getElementById('button');
          var counter = 5;
          var interval = setInterval(function() {
              if (counter === 0) {
                  button.style.display = 'block';
                  message.style.display = 'none';
                  clearInterval(interval);
              }
              message.innerHTML = 'You can view the url in ' + (counter--) + ' seconds.';
          }, 1000);
          button.onclick = function() {
              window.location = 'http://google.ch/';
          };
      };​
    </script>
    <style type="text/css">
      button { display:none; }​
    </style>
  </head>
  <body>
    <!-- (1) HTML element live inside the body. -->
    <span id="message">...</span>
    <button id="button">Visit Page Now</button>​
  </body>
</html>

最新更新