![](/img/trans.png)
[英]I am trying to understand why this JS snippet isn't working, could use some guidance adding to WP
[英]Why this snippet isn't working?
谁能帮助我找出我的小片段的毛病?
$(window).on("load resize", function() {
if ($(window).width() < 770) {
$(".side-by-side.right div").each(function() {
$(this).insertAfter($(this).parent().find('img'));
});
} else {
$(this).insertBefore($(this).parent().find('img'));
}
});
在else之上的第一个命令有效,但是在else不起作用之后,我在做什么错呢? 我肯定这是}或)之类的小事,或可能忘记加; 某处!
在else语句中,您尝试引用window对象的父对象。 这就是为什么它什么都不做的原因-窗口对象没有父对象。
第一部分(if)有效,因为.each()$(this)将引用您的div元素。 else中的$(this)仍指window。
修复它取决于您要尝试执行的操作-我真的无法告诉您。 但是,至少这就是为什么它不起作用的原因。 :-)
由于不清楚,您正在尝试实现什么,这可能是您正在寻找什么?
$(window).on("load resize", function() {
if ($(window).width() < 770) {
$(".side-by-side.right div").each(function() {
$(this).insertAfter($(this).parent().find('img'));
});
} else {
$(".side-by-side.right div").each(function() {
$(this).insertBefore($(this).parent().find('img'));
});
}
});
在else块中, this
参考窗口代替您要编辑的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.