使用jQuery.text()观察元素innerHTML更改

时间:2012-06-10 08:43:46

标签: javascript jquery text bind

当另一段代码在其上调用a时,我需要从.text('string')元素中删除一个类。我无法编辑其他代码:

function WPSetAsThumbnail(id, nonce){
    var $link = jQuery('a#wp-post-thumbnail-' + id);

    // this is where it's first called
    $link.text( setPostThumbnailL10n.saving );
    jQuery.post(ajaxurl, {
        action:"set-post-thumbnail", post_id: post_id, thumbnail_id: id, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
    }, function(str){
        var win = window.dialogArguments || opener || parent || top;
        $link.text( setPostThumbnailL10n.setThumbnail );
        if ( str == '0' ) {
            alert( setPostThumbnailL10n.error );
        } else {
            jQuery('a.wp-post-thumbnail').show();
            $link.text( setPostThumbnailL10n.done );
            $link.fadeOut( 2000 );
            win.WPSetThumbnailID(id);
            win.WPSetThumbnailHTML(str);
        }
    }
    );
}

非常感谢你对我的支持,即使这是一个非常棒的问题。

1 个答案:

答案 0 :(得分:1)

两个选项:

  1. 使用mutation events,但要注意它们已被弃用。
  2. 拉出DOM寻求更改。
  3. 第二个选项示例:

    var $element = $("#elementId");
    var lastText = $element.text();
    function checkForChanges()
    {
        var currText = $element.text();
        if (currText !== lastText)
        {
            alert('xxx');
            lastText = currText;
        }
    }
    
    var id = setInterval(checkForChanges, 500);
    
相关问题