Javascript 为什么这个片段不是';不行吗?
有人能帮我找出我的小片段有什么问题吗Javascript 为什么这个片段不是';不行吗?,javascript,jquery,Javascript,Jquery,有人能帮我找出我的小片段有什么问题吗 $(window).on("load resize", function() { if ($(window).width() < 770) { $(".side-by-side.right div").each(function() { $(this).insertAfter($(this).parent().find('img')); }); } else { $(this).ins
$(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'));
}
});
$(窗口).on(“加载调整大小”,函数(){
如果($(窗口).width()<770){
$(“.side-by-side.right div”).each(函数(){
$(this.insertAfter($(this.parent().find('img'));
});
}否则{
$(this.insertBefore($(this.parent().find('img'));
}
});
在else之上的第一个命令是有效的,但是在else之后的命令是无效的,我做错了什么?我敢肯定,这是一个很小的东西,像}或}的地方,或者可能忘记添加;某处
因为不清楚,你想要实现什么,这可能就是你想要的
$(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'));
});
}
});
$(窗口).on(“加载调整大小”,函数(){
如果($(窗口).width()<770){
$(“.side-by-side.right div”).each(函数(){
$(this.insertAfter($(this.parent().find('img'));
});
}否则{
$(“.side-by-side.right div”).each(函数(){
$(this.insertBefore($(this.parent().find('img'));
});
}
});
在else块中,
此
引用窗口而不是要编辑的对象。在else语句中,您尝试引用窗口对象的父对象。这就是为什么它什么都不做——窗口对象没有父对象
第一部分(if)有效,因为在该部分中.each()$(this)将引用div元素。else中的$(this)仍在引用窗口
修复它将取决于你想做什么——我真的不知道。但是,这至少是它不起作用的原因。:-) 如果缺少括号或其他内容,您可以通过查看浏览器的调试控制台来查看。e、 在firefox中使用ctrl-shift-J!谢谢你现在就尝试一下。请展示你的HTML或者这个答案解决了我的问题,谢谢你-但是,你知道如何将滚动条的宽度添加到窗口宽度吗?你可以尝试
window.innerWidth-document.documentElement.clientWidth
。或者,在使用jQuery时,$('body').innerWidth()代码>也应该起作用。我将if($(window.width()<770){替换为if($window.innerWidth-document.documentElement.clientWidth)<770){?window.innerWidth-document.documentElement.clientWidth
为您提供滚动条的宽度,您可以将其包含在计算中。我做了一些研究,但仍然没有实现的线索。您能帮我吗?