最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Cannot read property '0' of null - Stack Overflow

programmeradmin2浏览0评论

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 jsfiddle but not in my blog

blog url

the JavaScript code

<script type="text/javascript">
//<![CDATA[[
$(document).ready(function(){
    $('.widget-content').each(function(){
        var hg = $(this).html(),
            qsqs = hg.match(/[^[\]]+(?=])/g);
            alert(qsqs[0]);
    });
});
//]]>
</script>

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 jsfiddle but not in my blog

blog url

the JavaScript code

<script type="text/javascript">
//<![CDATA[[
$(document).ready(function(){
    $('.widget-content').each(function(){
        var hg = $(this).html(),
            qsqs = hg.match(/[^[\]]+(?=])/g);
            alert(qsqs[0]);
    });
});
//]]>
</script>
Share Improve this question asked Mar 4, 2014 at 0:49 abdelabdel 1031 gold badge2 silver badges12 bronze badges 4
  • console.log(qsqs, hg); – zerkms Commented Mar 4, 2014 at 0:49
  • can you see what's happen after I add your code Blog – abdel Commented Mar 4, 2014 at 0:53
  • Yes, I see that you're making wrong assumptions. If you checked console.log for your blog code (or used a debugger) - you could see it as well. – zerkms Commented Mar 4, 2014 at 0:54
  • @zerkms Thank you, I forgot to check if the code exist before I executing it. – abdel Commented Mar 4, 2014 at 0:59
Add a ment  | 

1 Answer 1

Reset to default 1

The problem is that you have another div with the class widget-content and it's causing the jquery selector to run in both.

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/679JJ/1/

发布评论

评论列表(0)

  1. 暂无评论