[英]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.