访问类名而不使用多个父方法

时间:2017-12-11 21:09:11

标签: jquery function parent closest

我试图找到一种更简洁的方法来访问“swatch-title-insert”div元素,而不使用下面函数中显示的多个父方法。我怎么能写得更好?

<!-- trying to access this element below in a cleaner way -->    
<div class="swatch-title-insert"></div>

<div class="carousel">
    <div class="jcarousel-clip">
        <ul class="product_swatch_list">
            <li class="product_swatch_list_item">
                <a href="http://www.test.com">
                    <span class="swatch_color" title="Green"></span>
                </a>
            </li>
            <li class="product_swatch_list_item">
                <a href="http://www.test.com">
                    <span class="swatch_color" title="Blue"></span>
                </a>
            </li>
        </ul>
    </div>
</div>     

$('span.swatch_color').each(function(){     
    $(this).mouseenter(function(){
        var swatchColor = $(this);
        var swatchTitle = swatchColor.attr('title');
        var swatchTitleInsert = swatchColor.parent().parent().parent().parent().parent().prev();
            swatchTitleInsert.html(swatchTitle);            
        });
});

0 个答案:

没有答案