jQuery:在不使用 id 的情况下获取对特定元素的引用
我正在对 jquery 进行一些修改,以在单击链接时显示隐藏的 div。这应该相当简单,但在本例中存在一个缺陷。我有以下标记:
<div class="first-row">
<div class="week">
<p>Uge 2</p>
<p>(08-01-11)</p>
</div>
<div class="destination">
<p><a href="#">Les Menuires</a></p>
<p>(Frankrig)</p>
</div>
<div class="days">4</div>
<div class="transport">Bil</div>
<div class="lift-card">3 dage</div>
<div class="accommodation">
<p><a class="show-info" href="#">Hotel Christelles (halvpension)</a></p>
<p>4-pers. værelse m. bad/toilet</p>
</div>
<div class="order">
<p><a href="#">2149,-</a></p>
<p class="old-price">2249,-</p>
</div>
<div class="hotel-info">
<!-- The div I want to display on click -->
</div>
</div>
当我单击“show-info”链接时,我希望显示“hotel-info”div。 我的后端开发人员不希望我使用 ids(不要问我为什么..)并且上面的标记被一遍又一遍地使用来显示数据。因此,我需要能够访问单击链接的“第一行”div 中的“酒店信息”div。
我尝试做类似的事情:
$(document).ready(function() {
$('.show-info').click(function() {
var parentElement = $(this).parent().parent();
var lastElementOfParent = parentElement.find(".show-hotel");
lastElementOfParent.show();
});
});
但没有结果:-/这可能吗?
非常感谢任何帮助!
预先非常感谢!
I'm tinkering a bit with jquery to show a hidden div when a link is clicked. This should be fairly simple, but there's a flaw to it in this case. I have the following markup:
<div class="first-row">
<div class="week">
<p>Uge 2</p>
<p>(08-01-11)</p>
</div>
<div class="destination">
<p><a href="#">Les Menuires</a></p>
<p>(Frankrig)</p>
</div>
<div class="days">4</div>
<div class="transport">Bil</div>
<div class="lift-card">3 dage</div>
<div class="accommodation">
<p><a class="show-info" href="#">Hotel Christelles (halvpension)</a></p>
<p>4-pers. værelse m. bad/toilet</p>
</div>
<div class="order">
<p><a href="#">2149,-</a></p>
<p class="old-price">2249,-</p>
</div>
<div class="hotel-info">
<!-- The div I want to display on click -->
</div>
</div>
When I click the "show-info" link I want the "hotel-info" div to display.
My backend devs don't want me to use ids (don't ask me why..) and the above markup is used over and over again to display data. Therefore I need to be able to access the "hotel-info" div in the "first-row" div where the link is clicked.
I've tried to do something like:
$(document).ready(function() {
$('.show-info').click(function() {
var parentElement = $(this).parent().parent();
var lastElementOfParent = parentElement.find(".show-hotel");
lastElementOfParent.show();
});
});
But without a result :-/ Is this possible at all?
Any help is greatly appreciated!
Thanks a lot in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
在我看来更好,因为如果每个“行 div”都具有相同的类(我假设只有第一个具有类
,那么它将独立于链接所在的行中的位置第一行
),你可以这样做:参考:
。最接近
,.siblings
解释为什么你的代码不起作用:
为您提供带有
.accommodation
类的div
,而该 div 没有带有.hotel-info
类的后代。无论如何,在超过一层的情况下使用这种遍历并不是一个好主意。如果结构稍微改变一下,你的代码就会崩溃。始终尝试使用不会因结构变化而中断的方法。
Try this:
Even better imo, as it would be independent from where the link is in a row, if every "row div" has the same class (I assume only the first one has class
first-row
), you can do:Reference:
.closest
,.siblings
Explanation why your code does not work:
gives you the
div
with class.accommodation
and this one has no descendant with class.hotel-info
.It is not a good idea to use this kind of traversal for more than one level anyway. If the structure is changed a bit, your code will break. Always try to use methods that won't break on structure changes.
你没有使用 ID 元素来查找你想要的 DIV 是对的:)
使用
closest
和nextAll
现场演示:http://jsfiddle.net/jomanlk/xTWzn/
You're right in not using an ID element to find the DIV you want :)
Use
closest
andnextAll
Live demo here : http://jsfiddle.net/jomanlk/xTWzn/