【问题标题】:How to Select Text in an Element (akin to highlighting with your mouse) on newly created ajax content?如何在新创建的 ajax 内容上选择元素中的文本(类似于用鼠标突出显示)?
【发布时间】:2012-10-13 17:15:56
【问题描述】:

我正在使用这里的selectText 函数https://stackoverflow.com/a/987376/784637

但是,当我在对新创建的元素发出 ajax 请求后立即调用此函数时:

selectText('some-newly-added-element')

我在 firebug 中收到以下错误

NS_ERROR_DOM_NOT_OBJECT_ERR: Parameter is not an object
[Break On This Error]   

range.selectNodeContents(text);

请注意,我可以在同一元素上的 ajax 请求之后调用此函数,就像这样

$('#container').on('click', '#some-newly-added-element', function(){
   selectText('some-newly-added-element');            
});

有没有办法在ajax请求完成后立即调用这个函数?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    回复:Is there a way to call this function immediately after the ajax request is done?

    你的意思是在ajax成功之后success。 或 http://api.jquery.com/jQuery.ajax/

    或这应该会有所帮助:Execute function after all ajax .load() requests are finished

    希望这符合原因:)

    示例

    $.ajax({
        url: this.html_url,
        cache: false,
        success: function(html){
            doSomething();
            return true;
        }
    });
    

    【讨论】:

    • 我试过这个,所以使用你的例子我做了result.html(html); selectText('some-newly-added-element'); ,但它仍然不起作用。请注意,#some-newly-added-element 是 html 响应的一部分
    • 嗯 @user784637 看看这里 man: stackoverflow.com/questions/2502828/… ;让我知道它是怎么回事! `:)~
    【解决方案2】:

    使用complete回调,将在ajax调用后触发

      $.ajax({
    
      complete: function(){
           selectText('some-newly-added-element');
          }
    
       });
    

    ajax 事件文档:http://docs.jquery.com/Ajax_Events

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 2013-07-07
      相关资源
      最近更新 更多