简体   繁体   English

使用.content()。get()。nodevalue;后在萤火虫中出现类型错误;

[英]Getting a typeerror in firebug after using .content().get().nodevalue;

i am trying to get a number out of each and every html LI inside the LI there are links as shown in below html code: 我试图得到一个数字出每个和李有如下图所示的HTML代码的链接内的每个HTML李:

<div id="summary">
        <ul class="nobullets last-child">
           <li><a href="#">Blog Posts</a> (9)</li>
           <li><a href="#">Photos</a> </li>
           <li><a href="#">Discussion</a> (10)</li>
       </ul>
</div>

As you can see there are numbers beside some of the links inside each of the LIs I want to get only those that have numbers (the blog posts) then store that number into a variable, if they dont have numbers it will just put a 0 into the variable. 如您所见,每个LI中的一些链接旁边都有数字,我只想获取那些具有数字的内容(博客文章),然后将该数字存储到变量中,如果它们没有数字,则只会放置一个0进入变量。

But here's my jquery code: 但是这是我的jQuery代码:

var getblog;
var getphoto;
      //blogs
      if($('#summary').find('ul li:first-child').contents().get(1).nodeValue != 'undefined'){
        getblog = $('#summary').find('ul li:first-child').contents().get(1).nodeValue;
        getblog = parseFloat( getblog.substr(2) );

      }
      else{
        getblog = '0';
      }


      //photos
      //+ li to get the second child
      if($('#summary').find('ul li:first-child + li').contents().get(1).nodeValue != 'undefined'){

        getphoto = $('#summary').find('ul li:first-child + li').contents().get(1).nodeValue;
        getphoto = parseFloat( getphoto.substr(2) );
      }
      else{
         getphoto = '0';
      }

firebug is throwing me with an error: TypeError: $("#summary").next().find("ul li:first-child + li").contents().get(1) is undefined firebug抛出一个错误: TypeError:$(“#summary”)。next()。find(“ ul li:first-child + li”)。contents()。get(1)未定义

Here's the jsfiddle http://jsfiddle.net/hP3Wq/ 这是jsfiddle http://jsfiddle.net/hP3Wq/

the blog is showing 9 which works but the photos is showing NaN instead of 0 博客显示9个有效,但照片显示NaN而不是0

I'm not sure what your code is all trying to accomplish, but if it's just matching eg the (3) prefix that's possibly apparent, you can just use .text() and .match() as follows: http://jsfiddle.net/hP3Wq/2/ . 我不确定您的代码都试图完成什么,但是如果它只是匹配例如可能很明显的(3)前缀,则可以按如下方式使用.text().match()http:// jsfiddle .net / hP3Wq / 2 /

$(document).ready(function(){
    // use a generic function so as not to repeat things
    var getNumber = function(index) {
        return ($("#summary li")  // from the lis
                .eq(index)        // a specific li from the lis
                .contents()       // its contents
                .last()           // number is apparent in the last text node
                .text()           // its text
                // match it against a regexp defining "(somenumbers)";
                // in case of no match, use a dummy array with zeroes
                // (this defines the fallback)
                .match(/\((\d*)\)/) || [0, 0]
               )[1];
    };
    var getblog = getNumber(0);
    var getphoto = getNumber(1);

    $('#summary').after(getblog + '<br>' + getphoto);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM