- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这段代码是从旧帖子中获得的,但它似乎不再起作用了,请帮忙。我什至无法再点击链接,但我的主要目标是让用户点击所有链接,然后将页面重定向到我网站上的另一个页面。
<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>
<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>
最佳答案
首先,修复您的 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>
关于javascript - 我希望页面在用户单击所有链接后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43407576/
对于我不断地用刽子手问题来打扰这个论坛,我深表歉意。我希望很快就能完成这件事。确实在这方面一直在努力。这是到目前为止我的代码: #include #include #include #defin
这篇文章是 this one 的延续. 我试图了解我是否是唯一一个错过并需要 .NET 泛型类型继承其泛型参数类型之一的能力的人。 面临的挑战是收集支持此功能的令人信服的理由,或者,要知道没有。 我给
我正在使用下面的代码设计一个 3 小时的 JavaScript 倒计时 var count = 10800; var counter = setInterval(timer, 1000); //1
我正在处理验证并放置一个 html 模式属性,并希望在不点击保存/提交按钮的情况下查看错误消息。当我在字段外单击时,它应该根据我的模式条件进行验证。 这是我的代码: Extra Credit Da
我有一个像这样的 mysql 表结构: id int primary key name varchar start_time float 数据可能是这样的: id name
我试图在桌面上保留一张带有描述的图片,然后在移动设备上希望图片在左边,描述在右边……更多的是尽可能让所有内容都在首屏。 我希望桌面上的 DIV 是: 一个一个 BBB 但是在手机上: A B A B
我通过 Stackoverflow 进行了搜索,发现很多人试图避免 CSS 列中的重叠,但实际上我被要求让文本和图像重叠。 更新: 更好的图像解释了我被要求做的事情,以及指向我正在使用的代码的链接。谢
我在我的框架中设置了几个协议(protocol)来处理资源。在其中一个协议(protocol)中,我设置了一个扩展来为 decode 函数提供默认实现。显示代码和发生的情况更简单(请参阅对 fatal
我想同时加载,或者在上面的图像之后立即加载。它会在图像和标题 div 在预加载器内旋转之前不断加载 - 不好!这是 HTML: ✕ Various inv
我目前正在开发一个小游戏。我使用 getKeyCode 来移动我的角色,但问题是我不希望你在按住按钮的情况下能够继续移动。无论如何,我是否可以使用 getKeyCode 仅在第一次单击时注册,然后在释
我需要设置 width 或 max-width 才能让 margin:auto 工作,对吗? Like in this jsFiddle . 问题是,段落宽度等于 max-width,因此文本 blo
您好,我在同一页面上有一个表单和一个 Paypal 订阅按钮。在重定向到 Paypal 付款之前,我想在单击提交表单(通过电子邮件发送给我)时订阅按钮。 > >
我想让用户在点击一个文本字段时感觉就像我们点击一个按钮,在释放按钮后它会在很短的时间内变成橙色,然后闪烁并再次变成它的第一种颜色。当我点击它时它运行良好但没有出现颜色闪烁。 布局文件 主要 Ac
我怎样才能让我的 MediaPlayer 继续播放,即使在手机锁定和屏幕关闭的情况下,我认为它可能必须做一些使其成为服务但不确定的事情。如果是这样,我该如何将其更改为服务,或者是否有更快更简单的修复方
有人可以帮助我理解内存泄漏的概念以及特定数据结构如何促进/防止它(例如链表、数组等)。前段时间我被两个不同的人教过两次——由于教学方法的不同,这让我有点困惑。 最佳答案 维基百科有一个 good de
我希望 HTML 表单在提交后不执行任何操作。 action="" 不好,因为它会导致页面重新加载。 基本上,我希望在按下按钮或有人在输入数据后点击 Enter 时调用 Ajax 函数。是的,我可以删
我正在执行一个程序 say A from another,首先是 fork-ing,然后是 execve()。现在的问题是我希望 A 使用我通常使用 LD_PRELOAD 来完成的库。我如何在 exe
我目前正在为我们公司测试 Autofac。 我们希望有以下规则: 如果接口(interface)只实现了一次,则使用 builder.RegisterAssemblyTypes 自动添加它(见下文)。
正如 Scott Meyers 和 Andrei Alexandrescu 在 this 中概述的那样文章 简单尝试实现双重检查锁定实现在 C++ 中特别是不安全的,并且通常在不使用内存屏障的多处理器
目前我有一些代码可以在点击时改变图像,但这段代码是在一个 php HTML 循环中所以事情变得棘手,点击一个开关会使每个故事的开关都激活,因为它是基于 CSS 运行的.如何让它们独立交换? $("
我是一名优秀的程序员,十分优秀!