首页 \ 问答 \ JQUERY在多个无序列表中选择第n个列表元素(JQUERY Selecting nth list element across multiple unordered lists)

JQUERY在多个无序列表中选择第n个列表元素(JQUERY Selecting nth list element across multiple unordered lists)

不幸的是,这不能用纯css来完成,因为nth-of-type只选择与元素的父( w3schools )的关系。 所以我正在寻找一个适用于多个列表的jquery替代方案。

HTML:

<div class="container" id="videos">
   <ul class="thumbnails">
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
   </ul>
   <ul class="thumbnails">
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
   </ul>
   <ul class="thumbnails">
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
   </ul>
</div>

JQUERY

$( "#videos li:nth-of-type(5n+1) img" ).css( "margin-left" , "0px");

此代码仍在选择:nth-​​of-type(5n + 1)与其父级(无序列表)相关。 我可以告诉,因为正在选择每个ul中的第一个列表项。 我需要在每个无序列表中选择第5个列表项,忽略它的父级。


Unfortunately, this can't be done with pure css because nth-of-type only selects on relation to an element's parent (w3schools). So I'm looking for a jquery alternative that works across multiple lists.

HTML:

<div class="container" id="videos">
   <ul class="thumbnails">
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
   </ul>
   <ul class="thumbnails">
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
   </ul>
   <ul class="thumbnails">
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
      <li class="tn">
         <img class="tn-img" src="img/thumbnails/placeholder.jpg" />
      </li>
   </ul>
</div>

JQUERY

$( "#videos li:nth-of-type(5n+1) img" ).css( "margin-left" , "0px");

This code is still selecting :nth-of-type(5n+1) in relation to it's parent (the unordered list). I can tell because the first list item in every ul is being selected. I need to select the 5th list item across every unordered list, disregarding it's parent.


原文:https://stackoverflow.com/questions/28117363
更新时间:2022-11-30 09:11

最满意答案

您需要将计时器的ID传递给clearTimeout

clearTimeout(timer);

另外,不要将字符串传递给setTimeout 。 通过功能:

var timer = window.setTimeout(redirect, 8000);
// Or
var timer = window.setTimeout(function() {
    redirect();
}, 8000);

You need to pass the ID of the timer to clearTimeout:

clearTimeout(timer);

Also, don't pass a string to setTimeout. Pass a function:

var timer = window.setTimeout(redirect, 8000);
// Or
var timer = window.setTimeout(function() {
    redirect();
}, 8000);

相关问答

更多
  • 你需要在函数之外声明“定时器”。 否则,您将在每个函数调用中获得一个全新的变量。 var timer; function endAndStartTimer() { window.clearTimeout(timer); //var millisecBeforeRedirect = 10000; timer = window.setTimeout(function(){alert('Hello!');},10000); } You need to declare timer outside ...
  • 在你的第一个问题中你问: clearTimeout()是否会停止计时器并且不执行计时器内的功能? 或者只是清除超时并立即执行该功能? 答案是第一个选项 - clearTimeout停止计时器并且不执行计时器回调。 代码问题中的问题是因为thisTimeout是在keyup函数中声明的。 keydown函数没有thisTimeout的引用。 您应该将其移动到共享范围: var thisTimeout; $('input').keyup(function(){ var thisClass = $(th ...
  • clearTimeout和clearInterval不应该是可互换的,因为它们分别是为setTimeout和setInterval设计的。 此外,setTimeout和setInterval都做不同的事情:setTimeout告诉一组命令等待执行,只执行一次,而setInterval重复执行,直到时间结束(当然,除非你调用clearInterval)。 clearTimeout将导致setTimeout在超时到期之前触发,而clearInterval将停止setInterval循环。 虽然它可能适用于一个浏 ...
  • 您需要将计时器的ID传递给clearTimeout : clearTimeout(timer); 另外,不要将字符串传递给setTimeout 。 通过功能: var timer = window.setTimeout(redirect, 8000); // Or var timer = window.setTimeout(function() { redirect(); }, 8000); You need to pass the ID of the timer to clearTimeout ...
  • 从我所知道的,你的代码没有任何问题。 但我想你可能忘了从中删除一行。 setTimeout(start, 5000); 无论何时/何时点击一个东西,都会在5秒后创建超时。 这可能会造成时间问题。 这是一个可能发生的情况: 你点击了 toggleTimer() - > start() - >创建超时和间隔 5s之后,你的setTimeout(start, 5000)执行并重新分配你的超时和间隔变量 你重新点击 最新的超时和间隔被清除,但不是第一个 为了安全起见,您可以在每个函数的开头清除您在所述函数中创建的超 ...
  • Node.js在单个线程中执行。 所以不会有任何竞争条件,并且可以在触发之前可靠地取消超时。 另请参阅相关讨论(在浏览器中)。 我正在谈论setTimeout时间实际到期并且解释器开始执行它的过程的情况 没有看过Node.js内部,我不认为这是可能的。 所有东西都是单线程的,所以当代码运行时,解释器不能“做任何事情”。 您的代码必须在触发超时之前返回控制权。 如果你在你的代码中放入一个无限循环,整个系统就会挂起。 这都是“合作多任务”。 Node.js executes in a single thread ...
  • 因为你错误地使用了clearTimeout() 。 您的代码需要类似于以下内容: var x = setTimeout("doStuff();", tempo); clearTimeout(x); 您当前正在使用tempo作为超时手柄,这就是为什么它不起作用。 Because you're using clearTimeout() incorrectly. Your code needs to resemble the following: var x = setTimeout("doStuff();", ...
  • 你有一个明确的订单或操作问题(除其他外): var toggleEditPanelTimeout; //value set to 'undefined'; happens first // resets timeout function function resetEditPanelTimeout() { clearTimeout(toggleEditPanelTimeout); //still undefined; happens ...
  • 您的iTimeoutID是.on()事件处理函数的局部变量,因此每次该函数完成时都会销毁它,并在下次重新创建时进行重建。 将它从该范围移到更高级别或全局级别,以便它可以在一个事件之间存活到下一个事件。 你可以这样做; var iTimeoutID = null; $(document).on('keyup', '#filter_query', function (event) { var iTypingDelay = 800, sFilterVal = $.trim($('#filt ...
  • http://jsfiddle.net/coma/y52Q2/1/ 调节器 app.controller('Main', function ($scope) { var delay; var lookup = function() { console.log($scope.input); }; $scope.lookup = function() { clearTimeout(delay); delay = setTi ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)