jquery使用css选择器来选取元素吗

html-css012

jquery使用css选择器来选取元素吗,第1张

是的,jQuery使用 CSS 选择器来选取元素。

jQuery 使用 CSS 选择器来选取 HTML 元素。例如:

$("p") 选取 <p>元素。

$("p.intro") 选取所有 class="intro" 的 <p>元素。

$("p#demo") 选取所有 id="demo" 的 <p>元素。

jQuery css()选择器是jquery中的一个功能。

用$("div").attr("class"),就可以实现;

参考如下例子:

 if ($(this).parent().first().attr("class") == "MyName") {

  temp = temp+"(Normal)"

  } 

 

alert($("#show").attr("class").indexOf("b"))

alert($("#show2").attr("class") == "myCls")

alert($("div:eq(0)").attr("class") == "myCls") 

 

<div id="show" class="a b c"></div>

<div id="show2" class="myCls"></div>