clone.html() 为空
我想知道这个错误消息是什么意思:
clone.html() is null
控制台引用了包含以下代码的行:
clone.filter('p').html(clone.html().replace('a','b').replace('x','y'));
这是我的代码片段:
var clone = $('div p, div ul').clone();
clone.filter('p').html(clone.html().replace('a','b').replace('x','y'));
我错过了什么? 我应该检查clone.html()是否为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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
clone.html() is null
意味着您的选择器$('div p')
没有找到任何内容clone.html() is null
means that your selector$('div p')
does not find anything如果您的选择器未找到任何内容,
clone.html()
将为 null。这是因为,当 jQuery 找不到任何内容时,它会返回一个空数组。此外,在集合上调用html()
只会获取第一项的html
值。您可能应该循环执行此操作。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, callinghtml()
on a set, will only get you thehtml
value of the first item. You should probably be doing this in a loop.