javascript 未捕获的类型错误:无法读取 null 的属性“0”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22160700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot read property '0' of null
提问by abdel
I've got "Uncaught TypeError: Cannot read property '0' of null" on the console log note that the code I use is work great on jsfiddlebut not in my blog
我在控制台日志上看到了“Uncaught TypeError: Cannot read property '0' of null”,请注意我使用的代码在jsfiddle上运行良好,但在我的博客中却没有
the JavaScript code
JavaScript 代码
<script type="text/javascript">
//<![CDATA[[
$(document).ready(function(){
$('.widget-content').each(function(){
var hg = $(this).html(),
qsqs = hg.match(/[^[\]]+(?=])/g);
alert(qsqs[0]);
});
});
//]]>
</script>
采纳答案by Hoijof
The problem is that you have another div with the class widget-content and it's causing the jquery selector to run in both.
问题是你有另一个带有类 widget-content 的 div,它导致 jquery 选择器在两者中运行。
Example
例子
<div class="widget-content">
[2][Sport][Two]
</div>
<div class="widget-content">
.
</div>
Here's the Fiddle updated so you can see the error clearly: http://jsfiddle.net/679JJ/1/
这是已更新的 Fiddle,因此您可以清楚地看到错误:http: //jsfiddle.net/679JJ/1/