javascript - 你可以在 jQuery 中使用 length() 按图像名称计算元素吗?

标签 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

谢谢!

最佳答案

当然可以:

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

引用:http://api.jquery.com/attribute-ends-with-selector/

关于javascript - 你可以在 jQuery 中使用 length() 按图像名称计算元素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23144275/

相关文章:

内部与外部构造函数的 JavaScript 类属性

javascript - 为 jQuery Mobile 设置大小?

javascript - 如何使用 jQuery :not selector for more than one element at a time

javascript - 关于将 javascript 事件与 innerHTML DOM 元素绑定(bind)的问题

javascript - 克隆链接并将其附加到容器,但附加整个 URL — 而不是链接文本

javascript - jQuery 字段选择

javascript - 在 Javascript 中有效地计算 Josephus 排列

jquery - 检查选择器是否存在的最佳方法是什么?

android - 移动到 SD 卡?

javascript - 使用 formData 的 Ajax 文件上传在某些文件上失败