jquery remove()没有删除

时间:2011-08-24 11:54:56

标签: javascript jquery dom-manipulation sharethis

我有以下代码允许我实现shareThis功能。我要做的是当点击此叠加层的共享关闭按钮时,我尝试删除共享此.share-span附带的此功能,然后重新初始化它,但remove()不会似乎从DOM中删除了.span-share

<script type="text/javascript">
function getShareData() {
    jQuery(".suit-gallery-btn").each(function(index){
        jQuery(this).children().remove('span');
        jQuery(this).append("<span class='share-span'></span>"); // ShareThis button will be inserted in this span, which we are appending to each <div class="suit-gallery-btn">
        var suitLink = jQuery(this).find('a'); // the "click more information" link. you will need the href and title from this element.
        console.log(suitLink);
        stWidget.addEntry({
            "service":"email",
            "element": jQuery(this).find('.share-span')[0],
            "title":suitLink.attr('title'),
            "type":"large",
            "text":suitLink.attr('title'),
            "image": suitLink.attr('href'),
            "summary":suitLink.attr('title'),
            "onhover": false
        });
    });
}

jQuery(document).ready(function() {
    getShareData();
    jQuery("#closeX, #greyScreen, .stCloseNew2, .close, .close2").live("click", function(){
        getShareData();
    });
});

    <div id="suit-gallery">
  <img src="../images/galleries/business/DSC_0055_sm.jpg" alt="Stylish button 3 business suit, beige lightweight high twist cool wool Holland &amp; Sherry" width="164" height="247" />
  <div class="suit-gallery-btn">
    <a href="../images/galleries/business/DSC_0055.jpg" rel="lightbox[business]" title="Stylish button 3 suit, beige lightweight high twist cool wool Holland &amp; Sherry from £695 choice of 90 designs and colours">Click for more information</a>
</div>
</div>

3 个答案:

答案 0 :(得分:2)

你误解了删除的语义。

您可以在需要删除的对象上调用remove,而不是从要删除它的父对象上调用。

类似的东西:

 jQuery('span.share-span', jQuery(this)).remove();

答案 1 :(得分:1)

从我所看到的,原始代码中没有span

在您remove()之前,您似乎正试图span appended

答案 2 :(得分:1)

更改此行:

jQuery(this).children().remove('span');

为:

jQuery(this).children('span.share-span').remove();

在此处查看此操作:http://jsfiddle.net/MarkSchultheiss/CUrXF/我稍微更改了它以显示原始范围,然后在调用函数时删除。