我在应用一种风格时遇到了麻烦!important.我试过了:
$("#elem").css("width", "100px !important");
这什么都不做 ; 没有任何宽度样式应用.是否有jQuery-ish方式应用这样的样式而不必覆盖cssText(这意味着我需要先解析它等等)?
编辑:我应该补充一点,我有一个样式表!important,我试图用!important样式内联覆盖一个样式,所以使用等等.width()不起作用,因为它被我的外部!important样式覆盖.
此外,将覆盖以前的值的值进行计算,所以我不能简单地创建另一个外部风格.
是否可以使用CSS3选择器:first-of-type选择具有给定类名的第一个元素?我的测试没有成功,所以我认为不是吗?
守则(http://jsfiddle.net/YWY4L/):
p:first-of-type {color:blue}
p.myclass1:first-of-type {color:red}
.myclass2:first-of-type {color:green}<div>
  <div>This text should appear as normal</div>
  <p>This text should be blue.</p>
  <p class="myclass1">This text should appear red.</p>
  <p class="myclass2">This text should appear green.</p>
</div><div class="commentList">
   <article class="comment " id="com21"></article>
   <article class="comment " id="com20"></article>
   <article class="comment " id="com19"></article>
   <div class="something"> hello </div>
</div>
我想选择#com19?
.comment {
    width:470px;
    border-bottom:1px dotted #f0f0f0;
    margin-bottom:10px;
}
.comment:last-child {
    border-bottom:none;
    margin-bottom:0;
}
只要我div.something在commentList中有另一个作为实际的最后一个孩子,那就不起作用了.在这种情况下是否可以使用last-child选择器来选择最后一次出现article.comment?
一个棘手的CSS选择器问题,不知道它是否可能.
让我们说这是HTML布局:
<div></div>
<div></div>  
<div></div>  
<div style="display:none"></div>
<div style="display:none"></div>  
我想选择div显示的最后一个(即不是display:none),这将是div给定示例中的第三个.请注意,div真实页面上的s 数量可能不同(甚至是display:none那些).
我不能告诉之间的区别element:first-child和element:first-of-type
比如说,你有一个div
div:first-child
→所有<div>元素都是其父母的第一个孩子.
div:first-of-type
→所有<div>元素都是<div>其父元素的第一个元素.
这似乎完全相同,但它们的工作方式不同.
有人可以解释一下吗?
有没有办法选择匹配(或不匹配)任意选择器的每个第n个孩子?例如,我想选择每个奇数表行,但是在行的子集中:
table.myClass tr.row:nth-child(odd) {
  ...
}
<table class="myClass">
  <tr>
    <td>Row
  <tr class="row"> <!-- I want this -->
    <td>Row
  <tr class="row">
    <td>Row
  <tr class="row"> <!-- And this -->
    <td>Row
</table>
但:nth-child()似乎只计算所有tr元素,无论它们是否属于"行"类,所以我最终得到一个偶数 "行"元素而不是我正在寻找的两个元素.同样的事情发生在:nth-of-type().
有人可以解释原因吗?
<ul>
    <li class="list">test1</li>
    <li class="list">test2</li>
    <li class="list">test3</li>
    <li>test4</li>
</ul>
如何选择具有类名称的"最后一个孩子":list?
<style>
    ul li.list:last-child{background-color:#000;}
</style>
我知道上面的例子不起作用,但是有什么类似的东西可以工作吗?
重要:
a)我不能使用ul li:nth-child(3),因为它可能发生在第四或第五位.
b)没有JavaScript.
任何帮助将不胜感激,谢谢!
有没有办法将css3限制nth-of-type为一个类?我有一些动态数量的section元素,不同的类指定了它们的布局需求.我想抓住每一个第三.module,但它似乎nth-of-type查找类元素类型,然后计算类型.相反,我想将它限制为只有.modules.
谢谢!
JSFiddle演示:http://jsfiddle.net/danielredwood/e2BAq/1/
HTML:
<section class="featured video">
    <h1>VIDEO</h1>
</section>
<section class="featured module">
    <h1>NOT A VIDEO</h1>
</section>
<section class="featured module">
    <h1>NOT A VIDEO</h1>
</section>
<section class="featured module">
    <h1>NOT A VIDEO (3)</h1>
</section>
<section class="featured module">
    <h1>NOT A VIDEO</h1>
</section>
<section class="featured module">
    <h1>NOT A VIDEO</h1>
</section>
<section class="featured module">
    <h1>NOT A VIDEO (6)</h1>
</section>
CSS:
.featured {
  width:31%;
  margin:0 0 20px 0;
  padding:0 3.5% 2em 0;
  float:left;
  background:#ccc; …问题在于CSS和HTML.这是一个带有示例代码的jsFiddle链接.
HTML
<ul>
    <li class"complete">1</li>
    <li class"complete">2</li>
    <li>3</li>
    <li>4</li>
</ul>
CSS
li.complete:last-child {
    background-color:yellow;
}
li.complete:last-of-type {
    background-color:yellow;
}
这些CSS行中是否应该使用"完整"类来定位最后一个li元素?
jQuery中的这个查询也没有以它为目标:
$("li.complete:last-child");
但是这个做了:
$("li.complete").last();
li {
  background-color: green;
}
li.complete:first-child {
  background-color: white;
}
li.complete:first-of-type {
  background-color: red;
}
li.complete:last-of-type {
  background-color: blue;
}
li.complete:last-child {
  background-color: yellow;
}<ul>
  <li class="complete">1</li>
  <li class="complete">2</li>
  <li>3</li>
  <li>4</li>
</ul>我试图在id或类'B'的元素中选择类'A'的第一个元素.我尝试过> +和first-child选择器的组合,因为它不是类元素'B'中的第一个元素.它的工作,但是...我试图覆盖某些默认的CSS和我有在服务器端没有控制权,它似乎是类"A"元素在不同的位置,有时产生.这是一个例子:
<class-C>
  <class-B> might have a different name
      <some-other-classes> structure and element count might differ
      <class-A></class-A> our target
      <class-A></class-A> this shouldn't be affected
      <class-A></class-A> this shouldn't be affected
  </class-B>
</class-C>
有时,"B"类的名称不同,"A"之前的元素也不同.那么有没有办法在元素'C'中选择第一个'A'?因为'C'级总是在那里.我不能使用+>和第一胎选择,因为"A"元素不同的路径,第一,但元素"C"是永远存在的,这将是一个很好的起点.