强制用户点击<a>顺序链接<ol>& lt; li>秩序
Force Users to Click <a> Links In Sequential <ol> <li> Order
我有一个订单列表,其中包含大约10个列表项,这些列表项具有与它们相关联的锚标记链接。我试图强迫用户只能点击父li的孩子一旦链接被访问。所以在某种意义上,我想强迫用户按ol生成的顺序点击li链接。
这是我的html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>titlePage</title>
<link rel="stylesheet" type="text/css" href="titlePage.css"></head>
<script src="../jsLibraries/jquery-3.1.0.js"></script>
<script src="titlePage.js"></script>
</head>
<body>
<img id="backgroundIMG" src="images/Chapter-v2.png">
<ol id="chapterNumbers">
<li><a href="../chapters/chapter0.html">Chapter 0 - Animate Content</a></li>
<li><a href="../chapters/chapter1.html">Chapter 1 - Animate Content 2</a></li>
<li><a href="../chapters/chapter2.html">Chapter 2 - Video Content</a></li>
<li><a href="../chapters/chapter3.html">Chapter 3 - Video Content 2</a></li>
<li><a href="../chapters/chapter4.html">Chapter 4 - Audio Content</a></li>
<li><a href="../chapters/chapter5.html">Chapter 5 - Blah</a></li>
<li><a href="../chapters/chapter6.html">Chapter 6 - Blah</a></li>
<li><a href="../chapters/chapter7.html">Chapter 7 - Blah</a></li>
<li><a href="../chapters/chapter8.html">Chapter 8 - Blah</a></li>
<li><a href="../chapters/exam.html">Exam</a></li>
</ol>
<header id="courseTitle"> 001234OOELNA - Example Course Using Adobe Animate </header>
</body>
</html>
这是我的jQuery JS,我开始:
$(document).ready(function(){
$("li").first().nextAll().click(function( event ) {
event.preventDefault();
});
});
下面是与被访问的锚标记相关的CSS:
a:visited {
color:white;
}
这是输出的链接:
http://skywest.theyard.space/index/titlePage.html谁能帮我弄清楚我在哪里走错了,以及如何使它一旦第一个链接被点击,第一个孩子的链接被激活(返回真?)等等,所以用户必须按顺序点击链接?
谢谢!Eric
如果用户点击链接,则导航出链接页面,所以没有任何技巧
所以这段代码应该工作,即使在返回页面与链接。并在关闭/打开浏览器后重新排序。
$(document).ready( function() {
if (!sessionStorage.active) {
sessionStorage.active = "1";
}
(function set() {
$("li a").each(function(idx, elm) {
if (sessionStorage.active != idx+1) {
$(elm)
.parent().fadeTo(1, 0.7)
.off("click").on("click", function(evt) {
evt.preventDefault();
});
} else {
$(elm)
.parent().fadeTo(500, 1)
.off("click").on("click", function() {
sessionStorage.active = parseInt(sessionStorage.active, 10) + 1;
set();
});
}
});
}());
});
版本2:
$(document).ready( function() {
if (!localStorage.active) {
localStorage.active = "1";
}
(function set() {
$("li a").each(function(idx, elm) {
if (localStorage.active < idx+1) {
$(elm)
.parent().fadeTo(1, 0.7)
.off("click").on("click", function(evt) {
evt.preventDefault();
});
} else if (localStorage.active == idx+1){
$(elm)
.parent().fadeTo(1000, 1)
.off("click").on("click", function() {
localStorage.active = parseInt(localStorage.active, 10) + 1;
set();
});
} else if (localStorage.active > idx+1) {
$(elm)
.parent().fadeTo(1000, 1)
.off("click").on("click", function() {
return true;
});
}
});
}());
});
或版本2的hyper "smplfd",部分灵感来自jefr
$(function(){var a=localStorage.a||0,l=$('a')
l.click(function(){if(l.index(this)>a)return false
if(l.index(this)==a)localStorage.a=++a})})
看看这个。(我很好地注释了我的代码,所以不需要解释。但是,如果你需要澄清,就告诉我……: D)
var $links = $("li a");
//simplify localStorage error checking
var supportsLS = true;
try {
//this will throw an exception if we can't use localStorage
//ie. the user, like me, disables cookies ... :)
localStorage.supportsLocalStorage = true;
}
catch (exc) {
supportsLS = false;
}
//initialize the first link they can use
if (supportsLS && localStorage.getItem("nextAvail") === null) {
localStorage.nextAvail = 0;
}
function onLinkClick (evt) {
/*
This handles a given click on a link.
*/
//no point in running rest of code--they don't have localStorage support
//OR we aren't allowed to use it
if (supportsLS === false) {
return;
}
//cache some info -- improve readability
var thisInd = $links.index(this);
var nextAvail = parseInt(localStorage.nextAvail);
//they can't view this link -- prevent it from working
if (thisInd > nextAvail) {
evt.preventDefault();
}
//if they clicked the last link they're allowed to click,
//update localStorage.nextAvail so they can click the next link.
else if (thisInd === nextAvail) {
localStorage.nextAvail = thisInd + 1;
}
}
//setup onclick handlers
$links.click(onLinkClick);
错误总结:
我最初处理"更新"localStorage.nextAvail
的方式是有缺陷的 plain错误。您可以检查代码以了解原因,但足以说明"正在更新"没有更新任何内容,并且在按预期方式使用该程序时,用户可能会意外地撤消其进度。
你的JS使除了第一个以外的所有链接都不可点击,不管它们是否被访问过。
不幸的是,你不能判断一个链接是否被访问过,基于js(见这个链接)。
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 如何使用jQuery自动打开页面上的所有链接
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- jquery动画可以通过编程链接吗
- 在单击href链接的同时下载文件
- 尽管链接成功并已成功下载,但未找到NPM模块
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- ReactJS映射:如何仅在url变量不为空时呈现html链接
- 单击页面上的链接后高度发生变化
- 将纯文本URL转换为可单击链接
- python-selenium-点击上升链接
- 一点javascript元编程&可链接的设置器
- ng视图外的链接重定向到ng视图内的页面
- 如何调用“;链接_;在onclick事件上使用Javascript
- 链接所有<a>Meteor
- 如何在Angular中的表的所有单元格中添加链接
- 通过链接重定向不;我不在jstree中工作
- 根据选项卡内部的文本链接中的哈希ID动态选择jqueryUI选项卡
- 单击超链接时,如何使用Google Maps API v3缩放地图
- 完全可链接的li元素