What is the replacement for the child selector?
I've come across something of a hack: http://meyerweb.com/eric/thoughts/2005/05/31/universal-child-replacement/
Using the 'star html' hack for IE (6 and below) in combination with this allows me to select the direct child. Let's say we want to apply a padding-top of 10px to the direct child, F, of E:
* html body E F
{
/* apply style here for IE 6 */
padding-top: 10px;
/* This applies the style to every F inside of E */
}
* html body E * F
{
/* undo style here */
padding-top: 0px;
/* This will undo the style set above for every F that has something in between itself and E, that is, every F besides the direct children of E */
}
I do appreciate your responses so far but as much as I hate to accept my own answer, this was the solution I eventually settled on. Thanks guys!
Nth-child() selector alternative solution in CSS
You can use the :first-child
and adjacent sibling selector. They work from IE7+.
Notes:
- You shouldn't be using
h1
tag as a direct child of anyul
. But if that's how your structure is and you can't modify it then you can use:first-child + li, :first-child + li + li
. :nth-child(-n+3)
generally would select the first 3 children of a parent. In your demo, since the first child is ah1
and since you've also attached the element type (li
) to thenth-child(-n+3)
theh1
doesn't get selected and only the twoli
that follow it are selected. The selector that I've mentioned in Note 1 will do the same (that is, it will select the first twoli
that follow the first child - which is, theh1
tag - irrespective of how many children theul
has).
Demo 1: (without any other tag directly inside ul
)
li:first-child,li:first-child + li { color: red;}
<ul> <li>first</li> <li>second</li> <li>third</li> <li>fourth</li> <li>and so on..</li></ul>
nth-child selector replacement in IE
Place this script at the end of your page. It assumes the first tbody found, but you could modify this to whatever offset (or to handle multiple tbodys), or even make things easier by targetting with a specific id.
<script>
(function(){
var
tby = document.getElementsByTagName('tbody')[0],
trs = tby.getElementsByTagName('tr'),
i, l = trs.length
;
for ( i=0; i<l; i++ ) {
if ( (i + 1) & 1 ) {
trs[i]
.getElementsByTagName('td')[0]
.style
.backgroundColor = '#f0f0f0'
;
}
}
})();
</script>
The above could be minified if you are worried about bytes.
jQuery alternative to nth-child selector that supports classes
You can filter the items based on the index they have in the parent, in relation to other items with the same class
$('.item').filter(function(_,item) { return ($(item).siblings('.item').addBack().index(item)+1) % 3 === 0;}).css('color','red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent"> <div class="randomclass">...</div> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div> <div class="randomclassdifferentname">...</div> <div class="item">Item 4</div> <div class="item">Item 5</div> <div class="item">Item 6</div> <div class="item">Item 7</div> ...</div><div class="parent"> <div class="anotherclass">...</div> <div class="item">Another item 1</div> <div class="item">Another item 2</div> <div>...</div> <div class="item">Another item 3</div> ...</div>
What is the new proper way to use a child selector with a context node in jQuery?
The reason they are saying:
Note: The
$("> elem", context)
selector will be deprecated in a future release. Its usage is thus discouraged in lieu of using alternative selectors.
Is due to the comma followed by the context in the selector. E.g. $("> elem")
is fine however, $("> elem", context)
will be deprecated.
$("> elem", context)
is the same as $(context + "> elem")
.
A correct way of obtaining children and grandchildren is
$("elem").children('.child').children('.grandchild');
or
context.children('.child').children('.grandchild');
or
context.find('> .child > .grandchild');
Alternative for nth-child for older IE browsers
You can use jQuery's :nth-child() selector;
$("li:nth-child(even)") // target even li's
$("li:nth-child(odd)") // target odd li's
$("li:nth-child(5n)") // target the fifth li
Simple HTML DOM - Child Selectors (CSS)
Well, this should (must, actually:)) work (tested on 4 divs):
foreach($html->find('div.element') as $element) {
$paragraph=$element->find('p',0);
if($paragraph==$element->first_child())
echo $paragraph;
}
IE8 :nth-child and :before
You can (ab)use the adjacent sibling combinator (+
) to achieve this with CSS that works in IE7/8.
See: http://jsfiddle.net/thirtydot/LvvNL/64/
/* equivalent to li:nth-child(1) */
#nav-primary ul li:first-child a {
border-top: 5px solid red;
}
/* equivalent to li:nth-child(2) */
#nav-primary ul li:first-child + li a {
border-top: 5px solid blue;
}
/* equivalent to li:nth-child(3) */
#nav-primary ul li:first-child + li + li a {
border-top: 5px solid green;
}
You cannot emulate more complex variations of :nth-child()
such as :nth-child(odd)
or :nth-child(4n+3)
with this method.
Related Topics
How to Prevent Fractional Pixels in an Element with Width Set to Auto
Incorrect Vertical Alignment in Ie8
CSS - Centering a Page - Then Making The Page 100% Height
Bootstrap Container-Fluid - Remove Margins The Right Way (Overflow)
Using CSS Text-Overflow to Vary The Number of Lines of Text Within an Element of a Set Height
Checkboxes Not Displaying Chrome - Work in Other Browsers
How to Specify The Use of Text or Titling Figures in CSS
Add Delayed Time in CSS3 Animation
Remove Underline in Inputtext in React Native
How to Increase Bootstrap 3 Navbar Height While Keeping Menu Height Small When Collapsed
Vue Binding Value Based on Media Query
How to Use Absolute Path to Import Custom SCSS, When Using React + Webpack
Float a Div in Top Right Corner Without Overlapping Sibling Header
Flex-Grow Not Working in Internet Explorer 11
How to Use The .Clearfix Class
Align Text Baseline with a Button in CSS