Возможно ли такое на js
На странице есть теги звездочек
<i class="glyphicon glyphicon-star"></i> Теги находятся в блоке <div class="ot_ratting"> <i class="glyphicon glyphicon-star"></i> <i class="glyphicon glyphicon-star"></i> <i class="glyphicon glyphicon-star"></i> <i class="glyphicon glyphicon-star"></i> <i class="glyphicon glyphicon-star"></i> </div> Возможно ли посчитать во всех блоках ot_ratting количество i и сложить их между собой чтоб получить общие количество на странице. |
Нашел что то подобное но оно не работает
<script> $(function(){ $(".ot_testimonial").each(function(indx, el){ $(".count",el).text($('i',el).length) }); }); </script> Вывести хочу так <div class"count_all">Общий рейтинг голосов <span class="count"></span> </div> |
Stas1985,
:-? $(".count").text($(".glyphicon-star").length) |
Не могу понять почему у меня не работает:help:
Тут http://jsfiddle.net/CYEVf/32/ пробую все работает <div class="ot_name"> Владимир</div> <div class="ot_ratting"> <i class="glyphicon glyphicon-star"></i> <i class="glyphicon glyphicon-star"></i> <i class="glyphicon glyphicon-star"></i> <i class="glyphicon glyphicon-star"></i> <i class="glyphicon glyphicon-star"></i> </div> Общий рейтинг голосов: <span class="count"></span> $(".count").text($(".glyphicon-star").length); в чем проблема пока не разобрался |
$(function() { $(".count").text($(".glyphicon-star").length) }); |
<script> $(function() { $(".count").text($(".glyphicon-star").length) }); </script> Общий рейтинг голосов: <span class="count"></span> Без изменений пусто и все |
Stas1985,
<!DOCTYPE html> <html> <head> <title>Untitled</title> <meta charset="utf-8"> <style type="text/css"> </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(function() { $(".count").text($(".glyphicon-star").length) }); </script> </head> <body> <div class="ot_ratting"> <i class="glyphicon glyphicon-star"></i> <i class="glyphicon glyphicon-star"></i> <i class="glyphicon glyphicon-star"></i> <i class="glyphicon glyphicon-star"></i> <i class="glyphicon glyphicon-star"></i> </div> <div class"count_all">Общий рейтинг голосов <span class="count"></span> </div> </body> </html> |
Stas1985,
вангую, звёздочек нет изначально на странице, тогда подсчитывать надо после функции, которая их создаёт, поставьте скрит вниз страницы, может поможет. |
А если и так не поможет
$(function() { setTimeout(function(){ $(".count").text($(".glyphicon-star").length); },1000); }); это называется костыль (скрипт должен начать идти и потихонечку прихрамывать) |
Вот так вот все работает хорошо
<script> $(function() { setTimeout(function(){ $(".count").text($(".glyphicon-star").length); $(".count_coment").text($(".ot_list").length); },1000); }); </script> А можно поделить .coun / .count_coment Допустим .coun = 15, .count_coment = 3 и тут получить 5. Заранее спасибо за ответы! |
Цитата:
|
Уточните пожалуйста по подробнее
Цитата:
|
Stas1985,
$(function() { setTimeout(function(){ $(".count").text($(".glyphicon-star").length); $(".count_coment").text($(".ot_list").length); alert($(".glyphicon-star").length/$(".ot_list").length); },1000); }); |
Сделал так
$(function() { setTimeout(function(){ $(".count").text($(".glyphicon-star").length); $(".count_coment").text($(".ot_list").length); $(".count_comentall").text($(".glyphicon-star").length/$(".ot_list").length); },1000); }); И это то что надо:dance: рони Большое спасибо вам за помощь! |
Настроил рейтинг на сайте _https://lik-astana.kz/
<script> $(function() { setTimeout(function(){ $(".count").text($(".glyphicon-star").length); $(".count_coment").text($(".ot_list").length); $(".count_comentall").text($(".glyphicon-star").length/$(".ot_list").length); },1000); }); </script> <div class="count_block_coment" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating"> <p>Рейтинг составлен на основе результатов голосования пользователей сайта www.lik-astana.kz</p> <p>Общий рейтинг <span class="count_comentall" itemprop="ratingValue"></span> из (<span class="count_coment" itemprop="reviewCount"></span> голосов).</p> <meta itemprop="worstRating" content="1"/> </div> Но рейтинг не проходит проверку, может подскажите что не так. Что означает эта строка },1000); не задержка ли это на срабатывания скрипта? вот данные мой проверки в валедаторе _https://search.google.com/structured-data/testing-tool?hl=ru#url=https%3A%2F%2Flik-astana.kz%2F |
Цитата:
Цитата:
|
Цитата:
|
Задержку убрал
<script> $(function() { $(".count").text($(".glyphicon-star").length); $(".count_coment").text($(".ot_list").length); $(".count_comentall").text($(".glyphicon-star").length/$(".ot_list").length); }); </script> но все ровно не успевает сработать |
Stas1985,
оставьте скрипт в том варианте, в котором он работал, и забейте на валидатор |
Часовой пояс GMT +3, время: 23:49. |