Javascript jquery根据其他类更改类元素的值
我有一个页面,上面有许多与下面相同类型的divJavascript jquery根据其他类更改类元素的值,javascript,jquery,ruby-on-rails,html-parsing,Javascript,Jquery,Ruby On Rails,Html Parsing,我有一个页面,上面有许多与下面相同类型的div <div class="post_right" data-icon = "arrow-r"> <p>Posted at:</p> <div class="post_time"> <%= post.created_at.strftime("%I:%M %P") %> </div> <div class="post_timer"></div>
<div class="post_right" data-icon = "arrow-r">
<p>Posted at:</p>
<div class="post_time">
<%= post.created_at.strftime("%I:%M %P") %>
</div>
<div class="post_timer"></div>
</div>
但是所有的“post_timer”类值都会被第一个post_时间值更新。任何方式,我都可以在整个页面中根据相应的“post\u time”类值更新post\u计时器值。您需要获得相同索引的
$(“.post\u time”)
.html
只会得到第一个,否则(如您所见)
只需将DOM遍历到适当的元素。由于在您的示例中,
.post\u time
元素似乎是.post\u timer
元素的前一个同级元素,因此您可以使用:
jQuery还有很多
$("document").ready(function() {
$(".post_timer").each(function(){
var post_time = $(".post_time").html()
$(this).html(post_time);
});
});
var post_time = $(".post_time").eq($(this).index()).html();
$(".post_timer").html(function() {
return $(this).prev().html();
// or more structure independent:
// return $(this).siblings('.post_time').html();
});
$("document").ready(function() {
$(".post_timer").each(function(){
var post_time = $(this).prev(".post_time").html();
$(this).html(post_time);
});
});