如何避免双重 jQuery 对象查找
我正在开发一个 jQuery 悬停代码片段,它将在悬停时添加标记,然后使用该类。这里是JS。
$('.port-item').hover(function(){
var $this = $(this);
var name = $($this.find("img")).attr('title');
$this.append('<div class="port-item-cover"><h3>' + name + '</h3><div>');
$($this.children(".port-item-cover")).fadeIn();
}, function(){
$($(this).children(".port-item-cover")).fadeOut();
});
HTML 标记非常简单:
<div class="port-item">
<a href="/portfolio/#/<?=$p['shortname']?>">
<img src="images/portfolio/p_<?=$p['shortname']?>0.jpg" title="<?=$p['title']?>">
</a>
</div>
两个问题:主要的一个是,如何避免 jquery $($(this).children("#element"))
中的双重查找来查找当前范围内的子元素?第二个功能相当丑陋,有更好的方法吗?
第二个问题是,检查之前是否已将其悬停过以及标记是否存在的最佳方法是什么,因此我不会在后续悬停中添加它。
I'm working on a jQuery hover code snippet that will add markup on hover and then work with the class. Here is the JS.
$('.port-item').hover(function(){
var $this = $(this);
var name = $($this.find("img")).attr('title');
$this.append('<div class="port-item-cover"><h3>' + name + '</h3><div>');
$($this.children(".port-item-cover")).fadeIn();
}, function(){
$($(this).children(".port-item-cover")).fadeOut();
});
The HTML markup is pretty simple:
<div class="port-item">
<a href="/portfolio/#/<?=$p['shortname']?>">
<img src="images/portfolio/p_<?=$p['shortname']?>0.jpg" title="<?=$p['title']?>">
</a>
</div>
Two Questions: the main one is, how do I avoid the double lookups in jquery $($(this).children("#element"))
to look up the child elements inside the current scope? It's pretty ugly in the second function, is there a better way?
The second question is what is the best way to check if this has been previously hovered over before and if the markup is there so I do not add it on subsequent hovers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个很容易。您不需要外部
$
调用。.find()
将返回一个 jQuery 对象。就这样写至于检测它之前是否已经悬停过,你必须在某个地方设置一个标志。这可能看起来像这样:
您可能需要根据您的具体情况来喜欢它。如果您将鼠标悬停在很多东西上,也许使用
.data()
设置标志会更好。编辑错过了一个问题。第三个答案:要判断 DOM 对象(标记)是否已经存在,您需要搜索它并检查长度,如下所示:
The first one is easy. You dont need the outer
$
call..find()
will already return a jQuery object. Just write it like thisAs for detecting if it has been hovered over previously, you would have to set a flag somewhere. That might look like this:
You may need to get fancy with that depending on your specific circumstance. Perhaps setting the flag using
.data()
would be better if you are hovering over a lot of things.EDIT Missed a question there. Third answer: to tell if the DOM object (markup) is there already you search for it and check the length like so:
所有 jQuery 遍历方法都已返回 jQuery 对象;您永远不需要编写
$($this.children())
。没有。
a.如果没有 mouseenter,Mouseleave 永远不应该触发。
b.如果没有任何匹配元素,则不会发生任何事情;您不会收到错误。
但是,您需要在动画结束后删除该元素;现在,您将在每次悬停时添加一个单独的元素。
请注意,您将
mouseenter
简化为All jQuery traversal methods already return jQuery objects; you never need to write
$($this.children())
.No.
a. Mouseleave should never fire without mouseenter.
b. If there aren't any matching elements, nothing will happen; you won't get an error.
However, you need to remove the element after the animation finishes; right now, you're adding a separate element on every hover.
Note that you simplify your
mouseenter
to对于第一个问题,请改用:
For the first question, use this instead: