Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么jqueryif-else语句不起作用?_Jquery_Ruby On Rails_If Statement - Fatal编程技术网

为什么jqueryif-else语句不起作用?

为什么jqueryif-else语句不起作用?,jquery,ruby-on-rails,if-statement,Jquery,Ruby On Rails,If Statement,我有一个if/else语句,它检查DOM中显示的图像最初是否小于600px宽,如果不是,它将调整为不同的宽度。然而,if/else语句似乎正在对所有图像执行此操作,而不仅仅是那些小于600px宽的图像。有时class.tagMap也不会出现(仅当if/else语句出现时)。知道为什么吗 jqueryif/else语句 $('span.i_contact').each(function() { var imgWidth = $(this).data('img-width'); if

我有一个if/else语句,它检查DOM中显示的图像最初是否小于600px宽,如果不是,它将调整为不同的宽度。然而,if/else语句似乎正在对所有图像执行此操作,而不仅仅是那些小于600px宽的图像。有时class.tagMap也不会出现(仅当if/else语句出现时)。知道为什么吗

jqueryif/else语句

$('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);       
});
$('span.i_contact')。每个(函数(){
var imgWidth=$(this).data('img-width');
如果(imgWidth<599){
var newWidth=((imgWidth/600)*300);
console.log(newWidth);
$('.iconnection').css({
“宽度”:新宽度
});    
} 
否则{
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=$('')
taggedNode.css({
“边框”:“5px实心橙色”,
“宽度”:位置宽度,
“高度”:位置高度,
“左”:XPO,
“顶级”:YPO
});
}  
var n=$(this.data('index');
$('.i_tagmap'+n).append(taggedNode);
});
$(“span.o_触点”)。每个(函数(){
var imgWidth=$(this).data('img-width');
如果(imgWidth<599){
var newWidth=((imgWidth/600)*300);
console.log(newWidth);
$('.oconnection').css({
“宽度”:新宽度
});    
}    
否则{
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=$('')
taggedNode.css({
“边框”:“5px纯绿”,
“宽度”:位置宽度,
“高度”:位置高度,
“左”:XPO,
“顶级”:YPO
});
} 
var n=$(this.data('index');
$('.o_tagmap'+n).append(taggedNode);
});
否则{
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=$('')
taggedNode.css({
“边框”:“5px纯绿”,
“宽度”:位置宽度,
“高度”:位置高度,
“左”:XPO,
“顶级”:YPO
});
} 
var n=$(this.data('index');
$('.o_tagmap'+n).append(taggedNode);
});
ERB(图像的生成方式)


“橙色”)%%>
“绿色”)%%>

要使代码仅更改小于600的图像,您应该更改

$('.iconnection').css({
    "width":newWidth
}); 


这样,它只会更改迭代中的图像,而不会更改具有匹配类的所有图像。

您是否可以使用手动中的数据创建一个小的工作小提琴?代码末尾是否有zombie
else
循环。是打字错误吗
$('.iconnection').css({
    "width":newWidth
}); 
$(this).css({
    "width":newWidth
});