Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何避免双重jQuery对象查找_Jquery_Jquery Selectors - Fatal编程技术网

如何避免双重jQuery对象查找

如何避免双重jQuery对象查找,jquery,jquery-selectors,Jquery,Jquery Selectors,我正在编写一个jQuery hover代码片段,它将在hover上添加标记,然后使用该类。这是JS $('.port-item').hover(function(){ var $this = $(this); var name = $($this.find("img")).attr('title'); $this.append('<div class="port-item-cover"><h3>' + name + '<

我正在编写一个jQuery hover代码片段,它将在hover上添加标记,然后使用该类。这是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();
});
$('.port项')。悬停(函数(){
var$this=$(this);
var name=$($this.find(“img”)).attr(“title”);
$this.append(“”+name+“”);
$($this.children(“.port item cover”).fadeIn();
},函数(){
$($(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))
中的双重查找以查找当前范围内的子元素?第二个函数很难看,有更好的方法吗


第二个问题是什么是最好的方法来检查之前是否已将其悬停在上面,以及标记是否在那里,这样我就不会在后续悬停时添加它

对于第一个问题,请使用此选项:

$(this).children("#element")
  • 所有jQuery遍历方法都已返回jQuery对象;您永远不需要编写
    $($this.children())

  • 没有

    a。Mouseleave在没有mouseenter的情况下不能开火

    b。如果没有任何匹配元素,则不会发生任何事情;你不会出错的

  • 但是,需要在动画完成后删除元素;现在,您正在每个悬停上添加一个单独的元素

    请注意,您可以将
    鼠标指针简化为

    $('<div class="port-item-cover"><h3>' + name + '</h3><div>')
        .appendTo(this)
        .fadeIn();
    
    $(''+名称+'')
    .附于(本)
    .fadeIn();
    
    第一个很简单。您不需要外部的
    $
    调用
    .find()
    将已返回jQuery对象。就这样写吧

    $(this).children("#element")
    
    至于检测它之前是否被悬停过,你必须在某处设置一个标志。可能是这样的:

    var hoveredOver = false;
    $('.port-item').hover(function(){
        hoveredOver = true;
        //continue event handler
    }
    
    根据你的具体情况,你可能需要对它产生幻想。如果您将鼠标悬停在很多东西上,那么使用
    .data()
    设置标志可能会更好

    $('.port-item').hover(function(){
        $(this).data('hoveredOver', true);
        //continue event handler
    }
    
    编辑遗漏了一个问题。第三个答案:要判断DOM对象(标记)是否已经存在,请搜索它并检查长度,如下所示:

    if($(this).find('#port-item-cover').length>0)
    {
        //element exists
    }
    else
    {
        //element does not exist, add it
    }
    

    好啊我仔细考虑了孩子们的情况。哈哈!谢谢大家!我不知道$(this).data(…)-我必须研究一下。$(this).data(…)如果您想向某个元素添加属性而不实际在元素本身上创建自定义HTML属性,那么它很有用。我用它作为标志,就像@Jeff用它做的一样。这是编写
    mouseenter
    更好的方法,因为它可以很好地控制范围。谢谢