强制用户点击<a>顺序链接<ol>& lt; li>秩序

Force Users to Click <a> Links In Sequential <ol> <li> Order

本文关键字:ol 链接 lt 秩序 li 顺序 用户      更新时间:2023-09-26

我有一个订单列表,其中包含大约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 -n代码

$(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(见这个链接)。