如何减少 jQuery 函数中使用的子级数量?
我觉得我必须在一些 jQuery 函数中使用太多的 .children()
。
这是我的 HTML:
<div class="goal-small-container">
<div class="goal-content">
<div class="goal-row">
<span class="goal-actions">
这是我的 jQuery:
$('.goal-small-container').hover(function() {
$(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "visible"});
}, function () {
$(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "hidden"});
});
有更好的方法吗? 如何减少 jQuery 函数中使用的子级数量?
I feel like I have to use way too many .children()
in some of my jQuery functions.
Here's my HTML:
<div class="goal-small-container">
<div class="goal-content">
<div class="goal-row">
<span class="goal-actions">
And here's my jQuery:
$('.goal-small-container').hover(function() {
$(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "visible"});
}, function () {
$(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "hidden"});
});
Is there a better way? How can I reduce the amount of children I use in my jQuery functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
或者更简单地说:
or more simply:
您听说过
.find()
吗?have you heard about
.find()
?代替
您可以使用:
: 具有完全相同的含义。但是,如果不可能出现歧义,(
.goal-actions
只会出现在该标记结构中),您只需使用find('.goal-actions')< /代码>。
Instead of
You can use:
For exactly the same meaning. If there's no chance of that being ambiguous, however, (
.goal-actions
will only appear in that structure of the markup) you can just usefind('.goal-actions')
.你可以只使用:
You can just use:
为什么不在第二个
上使用 .show() 和 .hide() ?并且,最初让它们显示隐藏或其他东西。
Why don't you just use .show() and .hide() on the second
<div>
? And, initially have them display hidden or something.