所以我创建了一个erb块,它获取每个图像标签的坐标,我想在其中显示所述坐标处每个图像的标签。但是,只显示一个标记,而不是迭代中的每个标记。知道为什么吗?它与.each()有关吗?
<% if @new_manual.present? %>
<% @new_manual.steps.each do |step| %>
<% i_connection = Contact.find(step.input_contact) %>
<span class="i_connection" data-pos-x="<%= i_connection.pos_x %>" data-pos-y="<%= i_connection.pos_y %>" data-pos-width="<%= i_connection.pos_width %>" data-pos-height="<%= i_connection.pos_height %>"> </span>
<br>
<div class='image_panel'>
<%= image_tag(i_connection.image.image.url(:large)) %>
<div class='planetmap'></div>
<% end %>
<% end %>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("span.i_connection").each(function() {
var pos_width = $(this).data('pos-width');
var pos_height = $(this).data('pos-height');
var xpos = $(this).data('pos-x');
var ypos = $(this).data('pos-y');
$(".tagged_box").css("display","block");
$(".tagged").css("border","5px solid red");
if ((xpos !== undefined) && (ypos !== undefined)) {
console.log('X:' + xpos + 'px' + ' ' + 'Y:' + ypos +'px');
$('.planetmap').append('<div class="tagged" style="width:'+pos_width+'px;height:'+
pos_height+'px;left:'+xpos+'px;top:'+ypos+'px;" ><div class="tagged_box" style="width:'+pos_width+'px;height:'+
pos_height+'px;display:none;" ></div>')
}
}); //END OF SPAN.CONNECTION ITERATION
});
修改 所以我将id更改为类,现在标签显示每张照片。成功!然而,它仍然显示它们,而不是它们受人尊敬的标签。我认为这与.each()方法有关。
编辑#2最新代码
该块迭代2个图像。 现在.tagged显示在两个图像上,而不是每个尊重图像的一个标记
<div class="container">
<% if @new_manual.present? %>
<% @new_manual.steps.each do |step| %>
<% i_connection = Contact.find(step.input_contact) %>
<span class="i_connection" data-pos-x="<%= i_connection.pos_x %>" data-pos-y="<%= i_connection.pos_y %>" data-pos-width="<%= i_connection.pos_width %>" data-pos-height="<%= i_connection.pos_height %>"> </span>
<br>
<div class="image_panel">
<%= image_tag(i_connection.image.image.url(:large)) %>
<div class='planetmap'></div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("span.i_connection").each(function() {
var pos_width = $(this).data('pos-width');
var pos_height = $(this).data('pos-height');
var xpos = $(this).data('pos-x');
var ypos = $(this).data('pos-y');
$(".tagged_box").css("display","block");
$(".tagged").css("border","5px solid red");
// if ((xpos !== undefined) && (ypos !== undefined)) {
// console.log('X:' + xpos + 'px' + ' ' + 'Y:' + ypos +'px');
$('.planetmap').append('<div class="tagged" style="width:'+pos_width+'px;height:'+pos_height+'px;left:'+xpos+'px;top:'+ypos+'px;" ><div class="tagged_box" style="width:'+pos_width+'px;height:'+
pos_height+'px;" ></div>')
// }
}); //END OF SPAN.CONNECTION ITERATION
});
</script>
<% end %>
<% end %>
答案 0 :(得分:0)
从您提供的代码中查看最终生成的HTML的内容有点困难,但跳出的一个错误是,当您的jQuery选择器正在查找时,span
的类为"i_connection"
类"connection"
的范围。这实际上应该导致.each()
根本不运行,所以这可能不是你的全部问题。
答案 1 :(得分:0)
我在您的代码中看到的一些问题:
<span class="i_connection"
,但正如杰森已经指出的那样使用$("span.connection").each(function() {
。 divs
具有相同的id
,即image_panel
和planetmap
。 id
在整个DOM文档中应该是唯一的。要解决第一个问题,您应该更改$("span.connection").each(function() {
以使用正确的类,即
$("span.i_connection").each(function() {
我假设您希望image_panel
div只显示<% if @new_manual.present? %>
评估为true。这解决了重复的id
问题之一。对于planetmap
上的第二个重复ID,我再次假设您希望它基于我看到的<% @new_manual.steps.each do |step| %>
代码而不在JS
之内。请按照以下方式更新您的erb,看看是否符合您的要求:
<% if @new_manual.present? %>
<div class='image_panel'>
<% @new_manual.steps.each do |step| %>
<% i_connection = Contact.find(step.input_contact) %>
<span class="i_connection" data-pos-x="<%= i_connection.pos_x %>" data-pos-y="<%= i_connection.pos_y %>" data-pos-width="<%= i_connection.pos_width %>" data-pos-height="<%= i_connection.pos_height %>"> </span>
<br>
<%= image_tag(i_connection.image.image.url(:large)) %>
<% end %>
<div id='planetmap'></div>
</div>
<% end %>