javascript - 使用 jQuery 和 CSS 在特定元素之前和之后为 child 设置不同的样式?

标签 javascript jquery css

我需要为 li.done 设置特殊样式,具体取决于它们是在 li.current 之前还是之后。我如何使用 jQuery 或 CSS 做到这一点?

ul { list-style: none; padding: 0; margin: 0; }

ul li {
  display:inline-table;
  padding: 5px 12px;
  color: #ddd;
  background-color: #bbb;
  margin: 0;
}
.done {
  background-color: #ddd;
  color: #aaa
}
.current {
  background-color: #99f;
  color: #dee
}
<ul>
<li class="done">step 1</li>
<li class="done">step 2</li>
<li class="current">step 3</li>
<li class="done">step 4</li>
<li>step 5</li>
</ul>

最佳答案

你应该看看 jQuerys .prevAll().nextAll()

var current = $(".current");
current.prevAll(".done").addClass("before");
current.nextAll(".done").addclass("after");

请注意,每次更改当前元素时都必须重做。

可以找到文档herehere .

关于javascript - 使用 jQuery 和 CSS 在特定元素之前和之后为 child 设置不同的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31688237/

相关文章:

php - Bitly API 导致意外的非法 token

php - 为什么使用 AJAX 调用 php 方法时会得到 undefined index ?

javascript 错误为 null 或不是对象

javascript - 如何更改我的输入类型范围的缩略图颜色?

css - 为什么我的 div 在 Safari 中不能水平对齐?

javascript - ObjectID 不存储十六进制值

javascript - Cucumber看不到javascript项目中的步骤

jquery - .load 调用 'facebook like box' 破坏了 jquery

html - 如何防止div移动另一个div

javascript - Chrome : Uncaught ReferenceError jQuery is not defined