[英]how to target the first div with jquery?
I am new to jquery, so please bear with me. 我是jquery新手,请耐心等待。 I need to find a way to always get the first div
from every series of divs with the same class that appears on a page so that I can add them some specific css styling to it. 我需要找到一种方法,始终从页面上出现的具有相同类的每个div
系列中获取第一个div
,以便可以向它们添加一些特定的CSS样式。 So technically I need something similar to :first-child
in css. 所以从技术上讲,我需要类似于CSS中的:first-child
。
I tried using .first()
but that only got me the first div
from the first series of 'divs' with that class name. 我尝试使用.first()
但是那只能使我从具有该类名称的第一个“ div”系列中获得第一个div
。
Example: 例:
<div class="expl">....</div> <!-- need to target this div-->
<div class="expl">....</div>
<div class="expl">....</div>
<h1>....</h1> - this is just an example, we can have any other html tag/content here
<div class="expl">....</div> <!-- and this div-->
<div class="expl">....</div>
<div class="expl">....</div>
<p>....</p>
<div class="expl">....</div> <!-- and this div-->
<div class="expl">....</div>
<div class="expl">....</div>
You can use this CSS selector: 您可以使用以下CSS选择器:
*:not(.expl) + .expl
This will target any .expl
element immediately following a non .expl
element. 这将针对任何.expl
立即是非以下元素.expl
元素。 Obviously you can use jQuery for that, or not, depending on your requirements. 显然,您可以根据需要使用jQuery,也可以不使用jQuery。
Try to wrap your div sets in another div like this: 尝试将div集包装到另一个div中,如下所示:
<div class="exp-wrapper">
<div class="expl">....</div> <!-- need to target this div-->
<div class="expl">....</div>
<div class="expl">....</div>
</div>
<h1>....</h1>
<div class="exp-wrapper">
<div class="expl">....</div> <!-- and this div-->
<div class="expl">....</div>
<div class="expl">....</div>
</div>
<p>....</p>
<div class="exp-wrapper">
<div class="expl">....</div> <!-- and this div-->
<div class="expl">....</div>
<div class="expl">....</div>
</div>
Then it would be easy to select first div inside wrapper. 然后,很容易在包装器中选择第一个div。
$(".exp-wrapper div:first")
UPDATE: Maybe something like this: 更新:也许是这样的:
$(".expl:first").add($(":not(.expl)").next(".expl"));
Based on Sunyatasattva's CSS answer, the jQuery equivalent is the following (using the Next Adjacent Selector of JQuery): 根据Sunyatasattva的CSS答案,等效的jQuery如下(使用JQuery的下一个相邻选择器 ):
var theThingsIWant = $(".expl:first").add(":not(.expl) + .expl");
A working example can be seen on this JsFiddle 可以在此JsFiddle上看到一个工作示例
To break it down: 分解:
$(".expl:first")
- find the very first div $(".expl:first")
-查找第一个div .add(":not(.expl) + .expl")
- also include any element with the expl
class which is immediately preceded by a sibling which is not an element with expl
class. .add(":not(.expl) + .expl")
-还包括与任何元件expl
即将由同级这是不与元素前面类expl
类。 See https://api.jquery.com/next-adjacent-Selector/ for a bit more detail. 有关更多详细信息,请参见https://api.jquery.com/next-adjacent-Selector/ 。
var divs = [$("div.expl").first(),
$("h1").first().next("div.expl").first(),
$("p").first().next("div.expl").first()];
This is a much more robust solution though. 但是,这是一个更加可靠的解决方案。
var selectAfter = function(what,whichIsAfter) {
return $(what).filter(function() {
return $(this).prev().not(what).is(whichIsAfter);
});
}
var divs = selectAfter("div.expl","*").add("div.expl:first");
jsfiddle - http://jsfiddle.net/5gTcC/1/ jsfiddle- http://jsfiddle.net/5gTcC/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.