我想在用户点击所有链接后重定向页面

时间:2017-04-14 08:00:41

标签: javascript jquery

我有这个代码我是从一个旧帖子中得到的,但它似乎不再起作用了,请帮忙。我甚至无法点击链接,但我的主要目标是让用户点击所有链接,然后将页面重定向到我网站上的另一个页面。



<script>
window.onload = function () {
    var anchors, clicked_count, clicked, i, cur_anchor, my_anchor_id;

    anchors = document.getElementsByTagName("a");
    clicked_count = 0;
    clicked = {};

    for (i = 0; i < anchors.length; i++) {
        cur_anchor = anchors[i];
        cur_anchor.setAttribute("link", i);
        cur_anchor.onclick = function (e) {
            e = e || window.event;
            e.preventDefault();

            my_anchor_id = this.getAttribute("link");
            if (!(my_anchor_id in clicked)) {
                clicked[my_anchor_id] = null;
                if (++clicked_count === anchors.length) {
                    console.log("WOULD BE REDIRECTING");
                    //window.location.href = "facebook.com";
                }
            }
        };
    }
};
</script>
&#13;
<a href="#Link1" id="link">Some Text 1</a>
<a href="#Link2" id="link">Some Text 2</a>
<a href="#Link3" id="link">Some Text 3</a>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

请立即试用

<a href="#Link1" class="link">Some Text 1</a>
<a href="#Link2" class="link">Some Text 2</a>
<a href="#Link3" class="link">Some Text 3</a>
wap = df['WAP']
wap = [float(x) for x in wap] #    lets make sure type is correct


for x in wap:
    while x < 8.49:
       print(x)
       break
    else:
       print('we hit it')
       break

答案 1 :(得分:2)

首先,修复您的HTML 您不能拥有多次id个属性,因为您有三次id="link"。请将其更改为class="link"以避免出现问题。

像这样:

<a href="#Link1" class="link">Some Text 1</a>
<a href="#Link2" class="link">Some Text 2</a>
<a href="#Link3" class="link">Some Text 3</a>

如果您使用的是jQuery,您可以使用更简单的代码实现您所描述的内容(即在点击三个不同的链接后重定向)。

下面是一个带注释的示例解决方案,它将检查哪些链接已被点击并仅在用户点击所有三个链接时重定向,无论他点击的顺序如何:

    $(document).ready(function() {

    // Create an array for clicked elements
    var clicked = [];
      
    // Create a click event for every link with class "link"
    $('.link').click(function() {

      // Check if we didn't clicked that link yet
      if (clicked.indexOf($(this).attr('href')) === -1) {

        // If we didn't add it's href attribute to "clicked" array"
        clicked.push($(this).attr('href'));
      }
        
      // If number of clicked links is 3, redirect
      if (clicked.length === 3) {
        console.log('REDIRECT');
        // location.href = 'http://put-your-url-here.com'
      }
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#Link1" class="link">Some Text 1</a>
<a href="#Link2" class="link">Some Text 2</a>
<a href="#Link3" class="link">Some Text 3</a>

答案 2 :(得分:0)

    window.onload = function () {
        var anchors, clicked_count, clicked, i, cur_anchor, my_anchor_id;

        anchors = document.getElementsByTagName("a");
        clicked_count = 0;
        clicked = [];

        for (i = 0; i < anchors.length; i++) {
            cur_anchor = anchors[i];
            cur_anchor.setAttribute("link", i);
            cur_anchor.onclick = function (e) {
                e = e || window.event;
                e.preventDefault();


                window.location.href = "https://facebook.com";
            }
        }

    };