选择类元素的不同方法

时间:2017-02-06 05:19:11

标签: jquery

我正在寻找在jQuery中选择类元素的所有可能性。

   $('div.wrap:first').
   $('div.wrap:last').

我知道我可以使用:

选择第一个和最后一个元素
[0], [1]

<Charting:LineSeries.DataPointStyle> <Style TargetType="Charting:LineDataPoint"> <Setter Property="Background" Value="Red" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Charting:LineDataPoint"> <Grid x:Name="Root" Opacity="0" /> </ControlTemplate> </Setter.Value> </Setter> </Style> </Charting:LineSeries.DataPointStyle> 似乎无法在jQuery中工作,或者我做错了。有没有其他可能选择元素(不是第一个和最后一个)?

5 个答案:

答案 0 :(得分:1)

您可以不使用

  $("div.wrap:not(:first):not(:last)").each(function(){....})

工作小提琴 https://jsfiddle.net/timus2001/qnvou2k7/

答案 1 :(得分:1)

$(".wrap")[1]

这会奏效。

$(".wrap")

返回一个数组。

你也可以试试..

$("[class='wrap']")
$("div[class='wrap']")
 $("[class^='wr']")

答案 2 :(得分:1)

请参阅CSS Selectors

第一个和最后一个

$("div.wrap:first-child")$("div.wrap:last-child")

第4个元素的

$("div.wrap:nth-child(4)")

对于偶数元素(第2,第4,第6等)

$("div.wrap:even")

对于(4n + 1)个元素(第1,第5,第9等)

$("div.wrap:nth-child(4n+1)")

$("div.wrap:not(:first-child):not(:last-child)")表示非首先,不是最后一个元素。

等等:)

答案 3 :(得分:1)

HTML DOM getElementsByClassName() Method

var x = document.getElementsByClassName("example");

$(".wrap")

答案 4 :(得分:0)

您也可以尝试java脚本代码

document.getElementsByClassName('wrap');