我有一个if / else语句,用于检查DOM中显示的图像是否最初小于600px宽,如果不是,则调整为不同的宽度。但是,似乎if / else语句对所有图像都这样做,而不仅仅是小于600px宽的图像。有时类似的.tagMap也不会出现(仅当if / else语句存在时)。知道为什么吗? Here's the HTML that is generated
jQuery if / else statement
$('span.i_contact').each(function() {
var imgWidth = $(this).data('img-width');
if (imgWidth < 599) {
var newWidth = ((imgWidth / 600)*300);
console.log(newWidth);
$('.iconnection').css({
"width":newWidth
});
}
else{
var pos_width = ($(this).data('pos-width')) / 2.025;
var pos_height = ($(this).data('pos-height')) / 2.025;
var xpos = ($(this).data('pos-x')) / 2.025;
var ypos = ($(this).data('pos-y')) / 2.025;
var taggedNode = $('<div class="tagged" />')
taggedNode.css({
"border":"5px solid orange",
"width":pos_width,
"height":pos_height,
"left":xpos,
"top":ypos
});
}
var n = $(this).data('index');
$('.i_tagmap' + n).append(taggedNode);
});
$("span.o_contact").each(function() {
var imgWidth = $(this).data('img-width');
if (imgWidth < 599) {
var newWidth = ((imgWidth / 600)*300);
console.log(newWidth);
$('.oconnection').css({
"width":newWidth
});
}
else {
var pos_width = ($(this).data('pos-width')) / 2.025;
var pos_height = ($(this).data('pos-height')) / 2.025;
var xpos = ($(this).data('pos-x')) / 2.025;
var ypos = ($(this).data('pos-y')) / 2.025;
var taggedNode = $('<div class="tagged" />')
taggedNode.css({
"border":"5px solid green",
"width":pos_width,
"height":pos_height,
"left":xpos,
"top":ypos
});
}
var n = $(this).data('index');
$('.o_tagmap' + n).append(taggedNode);
});
else {
var pos_width = ($(this).data('pos-width')) / 2.025;
var pos_height = ($(this).data('pos-height')) / 2.025;
var xpos = ($(this).data('pos-x')) / 2.025;
var ypos = ($(this).data('pos-y')) / 2.025;
var taggedNode = $('<div class="tagged" />')
taggedNode.css({
"border":"5px solid green",
"width":pos_width,
"height":pos_height,
"left":xpos,
"top":ypos
});
}
var n = $(this).data('index');
$('.o_tagmap' + n).append(taggedNode);
});
ERB(如何生成图像)
<div class="stepcontainer">
<div class="steptext">
<%= step.instruction %>
</div>
<div class="modalbutton">
<%= render(step.flags.new) %>
</div>
<% if step.input_contact.present? %>
<span class="i_contact i_contact<%= n %>" data-pos-x="<%= step.i_connection.pos_x %>" data-pos-y="<%= step.i_connection.pos_y %>" data-pos-width="<%= step.i_connection.pos_width %>" data-pos-height="<%= step.i_connection.pos_height %>" ="spanid<%= n %>" data-img-width="<%= step.i_connection.image.dimensions.first %>" data-index="<%= n %>"></span>
<div class="productimg">
<div class="image_panel<%= n %>" style="float:left; width:600px; position:relative;">
<%= link_to image_tag(step.i_connection.image.image.url(:medium), class: "iconnection" ), "#{step.i_connection.image.image.url(:large)}", class: "fancybox" %>
<div class="i_tagmap<%= n %>"></div>
</div>
</div>
<% if step.i_connection.cord? && !step.o_connection.dongle? %>
<div class="cableimg">
<%= image_tag(step.i_connection.cord_type.image.url(:thumb), :class => "orange") %>
</div>
<% end %>
<% end %> <!-- end of step.input_contact.present -->
<% if step.o_connection.cord? && !step.o_connection.dongle? %>
<div class="cableimg">
<%= image_tag(step.o_connection.cord_type.image.url(:thumb), :class => "green") %>
</div>
<% end %>
<span class="o_contact o_contact<%= n %>" data-pos-x="<%= step.o_connection.pos_x %>" data-pos-y="<%= step.o_connection.pos_y %>" data-pos-width="<%= step.o_connection.pos_width %>" data-pos-height="<%= step.o_connection.pos_height %>" id="spanid<%= n %>" data-img-width="<%= step.o_connection.image.dimensions.first %>" data-index="<%= n %>"> </span>
<div class="productimg">
<div class="image_panel<%= n %>" style="float:left; width:600px; position:relative;">
<%= link_to image_tag(step.o_connection.image.image.url(:medium), class: "oconnection"), "#{step.o_connection.image.image.url(:large)}", class: "fancybox" %>
<div class="o_tagmap<%= n %>"></div>
</div>
</div>
答案 0 :(得分:0)
要使代码仅更改小于600的图像,您应该更改
$('.iconnection').css({
"width":newWidth
});
到
$(this).css({
"width":newWidth
});
这样它只会改变你在迭代中所使用的图像而不是所有具有匹配类的图像。