点击3次后用javascript打开标签页

时间:2017-04-13 03:17:14

标签: javascript html button window

所以我想做的是当我点击按钮3次时会打开100个随机标签。我该怎么做?这是我尝试过的,而且我没有成功

 <script>
  counter = 0;
  document.getElementById('button').onClick = counter+=1;
  if (counter = 3) {
  window.open('www.youtube.com/')
  }
  </script>

我有一个名为按钮的按钮。这段代码不起作用。因此,非常感谢任何有关如何使按钮工作的想法。感谢

3 个答案:

答案 0 :(得分:1)

你在=犯了错误。将===替换为<!DOCTYPE html> <html> <body> <button id="button" onclick="myFunction()">Try it</button> <script> var counter = 0; function myFunction() { counter++; if (counter === 3) { window.open('www.youtube.com/') } } </script> </body> </html>

并且看看这个

    <form id="myForm">
        <input type="hidden" value="myForm" name="<?php echo ini_get("session.upload_progress.name"); ?>">
        <input type="file" id="file" />
        <a href="javascript:uploadFile()">Upload</a>
    </form>
    Upload:
    <div id="process">0%</div>

答案 1 :(得分:0)

如@bearwithbeard所示,从onClick调用函数通常是一种不好的做法。这是我的方法,

JS

var counter = 0
var the_button = document.getElementById('button');
the_button.addEventListener('click', increment)

function increment(){
    counter++
  if (counter == 3){
    window.open('http://youtube.com')
  }
}

HTML

<button id="button">
 Try me
</button>

您可以尝试here

修改 我真的不知道你为什么要打开100个标签(除了拖钓),浏览器很可能会阻止它。除此之外,你可以使用while循环,以防你仍然需要。

答案 2 :(得分:0)

我不知道您为什么要打开100个标签,但这里是打开2个标签的示例。

有两个变量:

  1. counter用于计算按钮被点击的次数。
  2. sites这是一系列要开放的网站。
  3. 的Javascript

    var counter = 0;
    var sites = ['http://www.google.nl', 'http://www.stackoverflow.com']; // URLS to open.
    function openTab()
    {
        counter++;
    
        var test = document.getElementById("count");
        test.innerHTML = 3-counter;
        if(counter == 3)
        {
            for(var i = 0; i < sites.length; i++)
            {
                window.open(sites[i]);
            }
    
            // Reset counter
            counter = 0;
        }
    }
    

    HTML

    <div>
        After <span id="count">3</span> click(s) there will be opened 2 tabs<br /><br />
        <button onclick="openTab();">Open</button>
    </div>
    

    这仅适用于教育,不得在您的网站中滥用或实施垃圾邮件。