Home >Web Front-end >HTML Tutorial >Jquery selector gets ID instance sharing through class name

Jquery selector gets ID instance sharing through class name

小云云
小云云Original
2018-02-28 11:23:012573browse

This article mainly shares with you the Jquery selector to obtain ID instances through class names. I hope it can help everyone.

Get by class name

To search by class, please add a . before the class name:

var a = $('.red'); // 所有节点包含 `class="red"` 都将返回
// 例如:
// <p class="red">...</p>
// <p class="green red">...</p>

Find objects that contain multiple class names at the same time, such as "car" ” and “ bus”

var a = $(&#39;.car.bus&#39;); // 注意没有空格!
// 符合条件的节点:
// <p class="car bus">...</p>
// <p class="blue bus car">...</p>

Get the ID of p through p’s class name

Sometimes we only know that p’s class name contains one of the names and want to get its id.

$(".tab-pane.active").attr("id")
// 符合条件的结果:
one
// <p class="tab-pane bus active" id="one">...</p>

Get the class of p through the ID name of p

$(".tab-pane.active").attr("id")
$("p #hrefMenu0").attr("class")
// 符合条件的结果:
tab-pane bus active
// <p class="tab-pane bus active" id="hrefMenu0">...</p>

Get the ID of the subset node under p through the class name of p

$(".tab-pane.active").children().attr("id")// 获取到以下两个id:content,page<p class="tab-pane active">
      <p id="content"></p>
      <ul id="page"></ul>
</p>

Get the class name of p Get the ID of the peer node under the p

$(".tab-pane.active").next().attr("id")// 获取到以下id:content<p class="tab-pane active">
</p>
 <p id="content"></p>

Related recommendations:

Detailed explanation of how to handle special symbols in the jQuery selector

Detailed explanation of jquery selector practice

jQuery selector sharing examples of attribute filter selectors

The above is the detailed content of Jquery selector gets ID instance sharing through class name. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn