Javascript-форум (https://javascript.ru/forum/)
-   Элементы интерфейса (https://javascript.ru/forum/dom-window/)
-   -   Возможно ли такое на js (https://javascript.ru/forum/dom-window/74073-vozmozhno-li-takoe-na-js.html)

Stas1985 11.06.2018 07:22

Возможно ли такое на 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 и сложить их между собой чтоб получить общие количество на странице.

Stas1985 11.06.2018 08:03

Нашел что то подобное но оно не работает
<script>
$(function(){
$(".ot_testimonial").each(function(indx, el){
$(".count",el).text($('i',el).length)
});
});
 </script>

Вывести хочу так
<div class"count_all">Общий рейтинг голосов <span class="count"></span> </div>

рони 11.06.2018 09:34

Stas1985,
:-?
$(".count").text($(".glyphicon-star").length)

Stas1985 11.06.2018 09:38

Не могу понять почему у меня не работает: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);

в чем проблема пока не разобрался

ruslan_mart 11.06.2018 09:41

$(function() {
   $(".count").text($(".glyphicon-star").length)
});

Stas1985 11.06.2018 09:50

<script>
$(function() {
   $(".count").text($(".glyphicon-star").length)
});
</script>
Общий рейтинг голосов: <span class="count"></span>

Без изменений пусто и все

рони 11.06.2018 09:57

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>

рони 11.06.2018 10:09

Stas1985,
вангую, звёздочек нет изначально на странице, тогда подсчитывать надо после функции, которая их создаёт, поставьте скрит вниз страницы, может поможет.

j0hnik 11.06.2018 14:21

А если и так не поможет

$(function() {
setTimeout(function(){
   $(".count").text($(".glyphicon-star").length);
},1000);
});


это называется костыль (скрипт должен начать идти и потихонечку прихрамывать)

Stas1985 12.06.2018 08:35

Вот так вот все работает хорошо
<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.
Заранее спасибо за ответы!


Часовой пояс GMT +3, время: 15:34.