你能用jQuery中的length()按图像名计算元素吗?

时间:2014-04-17 22:06:22

标签: javascript jquery html css

我正在尝试整理一个评论“摘要”部分,总结5星级,4星级,3星级评论等的数量。我需要做的是计算每个“评论级别”的实例产品页面并输出数字。例如如果一个产品有50条评论,其中14条是5星,我需要将该数字输出到页面为“14”。

评论的唯一区别不在于ID或类,而在于它输出的图像名称(即星数)。请参阅下面的图像文件名,我的意思是:

<ol class="ProductReviewList">
                <li class="">
        <h4 class="ReviewTitle">
            Cool hammock!
            <img src="http://cdn2.bigcommerce.com/rc71b9995f4a706510d16ad47d2472c26eb88e9bf/themes/HealthBeauty/images/IcoRating4.png" alt="">
        </h4>
        <p class="Meta">
            Posted by Matina Keller on 6th Jan 2014
        </p>
        <p>I love this hammock! I got the single brazilian for my courtyard out the side and it fits snug between two patio posts. I can easily put it up and take it down whenever i want. It's really comfortable and easy to fall asleep in. The material is of very high quality and far better than my old hammock. Delivery was about 4 days to adelaide FYI.</p>
        <hr>
    </li>   <li class="Alt">
        <h4 class="ReviewTitle">
            So comfortable!
            <img src="http://cdn2.bigcommerce.com/rc71b9995f4a706510d16ad47d2472c26eb88e9bf/themes/HealthBeauty/images/IcoRating5.png" alt="">
        </h4>
        <p class="Meta">
            Posted by Kendra Lovell on 17th Dec 2013
        </p>
        <p>I bought one of these for my daughter and she loves it. It's really comfortable and the colours are so pretty. Thanks siesta hammocks.</p>
        <hr>
    </li>   <li class="">
        <h4 class="ReviewTitle">
            Great customer service and product!
            <img src="http://cdn2.bigcommerce.com/rc71b9995f4a706510d16ad47d2472c26eb88e9bf/themes/HealthBeauty/images/IcoRating5.png" alt="">
        </h4>
        <p class="Meta">
            Posted by Susan Knight on 20th Nov 2013
        </p>
        <p>I must have had a million questions about this but the staff on the online chat answered all of them! Really good customer service and ordering system. The hammock itself is really good quality. So relaxing in the afternoon sun. Got the double hook kits and put it up between some wooden posts we already had under our pergola. Got the frame too in case we want to put it somewhere else. Really easy to assemble and is very sturdy. All up it's been a great experience shopping with siesta so thanks.</p>
        <hr>
    </li>
        </ol>

最后看看图片是IcoRating4.png还是IcoRating5.png

我想知道我是否可以使用.length()来计算文档中的元素,而不是通过id或类,但是通过文件名中的字符串,或者通过图像名称或类似的变体来计算。这可能吗?

I've put together a fiddle here if that helps

谢谢!

1 个答案:

答案 0 :(得分:3)

当然可以:

var count = $('img[src$="IcoRating4.png"]').length;

参考:http://api.jquery.com/attribute-ends-with-selector/