开发者

clone.html() is null

I'm wondering what this error message means:

clone.html() is null

The console refers to the line with following code:

clone.filter('p').html(clone.html().replace('a','b').replace('x','y'));

This is my code-snippet:


var clone = $('div p, div ul').clone();
clone.filter('p').html(clone.html().replace('a','b').replace('x','y')); 

What did I开发者_运维问答 miss? Shall I check whether clone.html() is null or not? Or is it a dirty solution? Any idea or hint is highly appreciated.


clone.html() is null means that your selector $('div p') does not find anything


clone.html() will be null, if your selector doesn't find anything. This is because, when jQuery finds nothing, it returns a blank array. Also, calling html() on a set, will only get you the html value of the first item. You should probably be doing this in a loop.

var clone = $('div p, div ul').clone();
clone.each(function(){
    var $this = $(this);
    if($this.html() != ''){
       $this.html($this.html().replace('a','b').replace('x','y'));
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜