选择第一个子项';使用Jquery的子对象
我的html结构如下:选择第一个子项';使用Jquery的子对象,jquery,select,find,children,Jquery,Select,Find,Children,我的html结构如下: <div class="one"> <div id="two"> <h2>Any</h2> <h4>Any</h4> </div> <div id="three"> </div> </div> 最终和有效(无闪烁!)代码为: 我想在来这里提问之前,我必须给自己更多的时间思考。总之,我学习了.andSelf()和一些Jqu
<div class="one">
<div id="two">
<h2>Any</h2>
<h4>Any</h4>
</div>
<div id="three">
</div>
</div>
最终和有效(无闪烁!)代码为:
我想在来这里提问之前,我必须给自己更多的时间思考。总之,我学习了.andSelf()和一些Jquery语法。再次感谢。您不需要同时使用
find()
和contents()
-尝试以下方法:
$(document).ready(function() {
$(".one").mouseover(function () {
$(this).children('div:first').children('h2,h4').stop(true, true).fadeIn('fast');
});
$(".one").mouseout(function () {
$(this).children('div:first').children('h2,h4').stop(true, true).fadeOut('fast');
});
});
如果您还需要在节点集中保留“第一个”元素,请在第二个.children()
之后使用.andSelf()
(根据@Felix的回答)。这:
$(this).children('div:first').contents().find('h2,h4').stop(true, true).fadeIn('fast');
致:
您可以使用和:
如果不想将第一个子项(父项)添加到集合中,请忽略
和self
。你的问题在这一点上有点含糊。但是我假设你实际上不想选择第一个孩子,只选择它的孩子。如果隐藏一个元素,它的子元素也会被隐藏。这实际上有点错误,因为他的代码容易出错。他只需要子元素,而不是子元素,因此find('h2,h4')返回错误的元素集。实际上,对于他所遇到的问题来说,这并不重要。没关系。谢谢大家,我似乎不必将h2、h4包含在淡入淡出/淡出div中-我最初的问题是,悬停在淡入淡出的h2和h4元素上会使div闪烁。不用担心。很高兴你把它分类了:)@elbatron:那么你想对子元素和元素本身调用fadeOut
?它的目的是什么?如果你隐藏父母,孩子也会被隐藏。谢谢。实际上,我想选择第一个元素及其子元素。我以前有过:'$(this).children('div:first').stop(true,true).fadeOut('fast');'它和上面的代码一样,至少结果是一样的。。。看起来它只选择第一个元素,而不选择其子元素:。。。虽然div fadein和out正确地悬停在h2和h4元素上,但是div会闪烁。。。由于这种闪烁,我认为我需要在Jquery中也包括第一项的子项…@elbatron:Mmmh。好吧,现在你至少知道如何选择所有这些选项,并且可以到处玩;)是的,谢谢你。有没有其他方法可以防止闪烁?(选择全部或仅选择第一个子div结果)
$(document).ready(function() {
$(".one").mouseover(function () {
$(this).children('div:first').children('h2,h4').stop(true, true).fadeIn('fast');
});
$(".one").mouseout(function () {
$(this).children('div:first').children('h2,h4').stop(true, true).fadeOut('fast');
});
});
$(this).children('div:first').contents().find('h2,h4').stop(true, true).fadeIn('fast');
$(this).children('div:first').children('h2,h4').stop(true, true).fadeIn('fast');
$(".one").mouseover(function () {
$(this).children().first().children('h2, h4').andSelf()....
});