选择器 $(body) 有效但破坏了代码(引用错误),而 $(“body”) 没有显示错误但不起作用
Selector $(body) works but break the code (reference error) while $("body") shows no error but dont work
我有一个JS函数,我需要在窗口调整大小时调用它。我发现使其工作的唯一方法是使用:
$(body).on('resize', function(){
myFunction();
});
有了这个,myFunction() 在窗口大小调整时执行。问题是我收到一个引用错误(正文未定义),所以我的 JS 的其余部分没有正确执行。
我尝试了不同的选择器:$("body"),$("document.body"),$(window),$(document),...有了它们,我没有收到引用错误,但函数没有执行。
这是看起来像(最小视图)我的html:
<html>
<head>
<script src="jquery.js"></script>
</head>
<body>
[ Alot of things ...]
<script src="main.js"></script>
</body>
</html>
因此,jQuery包含在我的头部部分中,而我的JS包含在关闭正文标签之前。
现在这看起来像我的主文件.js:
myFunction(){
alert("Hello world");
}
$(body).on('resize', function(){
myFunction();
});
$(document).ready(function(){
myFunction();
}
函数在页面加载时正常工作(带有 $(document).ready ),但我需要它在每次页面调整大小时运行。我搜索了很多来解决我的问题,但我被困住了。如果答案微不足道,我深表歉意。我是一个拥有JS和DOM元素选择器的菜鸟。
使用更多信息进行编辑:有问题的函数用于将多个div 设置为相等的高度。你可以在这里找到它 Equal Heightdivs with jQuery.我的div 组是一行 skelJS 网格系统。
所以最初的问题是,当我调整窗口大小时,divs 没有调整大小以适应响应式字体大小,所以我会溢出。显然,我试图在窗口/正文调整大小时调用该函数,但我无法让它工作,除非我使用 $(body) 选择器,这给了我一个引用错误,但随后溢出问题消失了,函数在调整大小时正常运行。真的很奇怪。我尝试了您的所有解决方案,似乎没有任何效果。
调整大小附加到窗口,而不是 document.body。
(function() {
function resizeFnc() {
console.log("resized called");
}
$(window).on("resize", resizeFnc); //calls it on resize
$(resizeFnc); //calls it on document ready
}());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$(body)
失败的原因是没有声明变量"body"。$("body")
和$(document.body)
失败的原因是该事件未在主体上触发,因此不调用它。
以及您链接到的代码。
function equalHeight(group) {
console.log("I was called");
tallest = 0;
group.each(function() {
thisHeight = $(this).height();
if(thisHeight > tallest) {
tallest = thisHeight;
}
});
group.height(tallest);
}
$(function(){
equalHeight($(".EqHeightDiv"));
});
$(window).on("resize", function(){
console.log("resized called");
equalHeight($(".EqHeightDiv"));
});
.EqHeightDiv { width: 30% ;box-sizing: border-box; display: inline-block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="EqHeightDiv" style="float:left;border:solid 1px #ccc;margin:10px;">Here is some stuff</div>
<div class="EqHeightDiv" style="float:left;border:solid 1px #ccc;margin:10px;">Here is some stuff<br />Here is some stuff</div>
<div class="EqHeightDiv" style="float:left;border:solid 1px #ccc;margin:10px;">Here is some stuff<br />Here is some stu<br />Here is some stuff</div>
现在损坏的是你正在使用的脚本。调用调整大小,它只是没有设置新的最大高度。因此,当触发所述窗口大小调整时,问题出在其他地方。
那么我们如何解决这个问题呢?
var timer;
function equalHeight(group) {
if (timer) window.clearTimeout(timer); //so this code is not called on every single resize
timer = window.setTimeout( function () {
var tallest = 0;
group.height(""); //reset the heights
group.each(function() {
thisHeight = $(this).height();
if(thisHeight > tallest) {
tallest = thisHeight;
}
});
group.height(tallest);
}, 10);
}
$(function(){
equalHeight($(".EqHeightDiv"));
});
$(window).on("resize", function(){
equalHeight($(".EqHeightDiv"));
});
.EqHeightDiv { width: 30% ;box-sizing: border-box; display: inline-block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="EqHeightDiv" style="float:left;border:solid 1px #ccc;margin:10px;">Here is some stuff</div>
<div class="EqHeightDiv" style="float:left;border:solid 1px #ccc;margin:10px;">Here is some stuff<br />Here is some stuff</div>
<div class="EqHeightDiv" style="float:left;border:solid 1px #ccc;margin:10px;">Here is some stuff<br />Here is some stu<br />Here is some stuff</div>
试试这个。窗口是全局对象,不应有任何引用错误
function myFunction(){
alert("Hello world");
}
$(window).resize(function() {
myFunction();
});
$(document).ready(function(){
$(window).on('resize', function(){
myFunction();
});
});
这是在行动:https://jsfiddle.net/6t987c28/
如果你想改变或添加一个类到<body>
,那么它应该在你的函数中完成,而不是调整jQuery的大小:
$(window).on('resize', function() {
myFunction();
});
function myFunction() {
$('body').css('background', 'green');
// or
$('body').addClass('selector-name');
}
- Node.js v6.2.0类扩展不是函数错误
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 我如何修复包含在captcha的addthis中的错误
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 节点是否需要模块传递带有方括号的arg?这是个错误吗
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- CKFinder 3为所选文件返回错误的URL
- 同位素库错误:未捕获错误无布局模式包装生产线8
- 铬:“;未捕获的语法错误:意外的标记:"
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 相位器状态未捕获参考错误
- NodeJS:zlib.gunzip(body)错误地返回“undefined”
- 选择器 $(body) 有效但破坏了代码(引用错误),而 $(“body”) 没有显示错误但不起作用
- IE 8中的jQuery错误:消息:'this.0.ownerDocument.body'为null或不
- 未捕获的错误无法找到模块'./template.body.js
- 抛出TypeError错误:null不是一个对象(计算"$("body").append&
- internet explorer 8-在IE8中使用JavaScript将CSS附加到Body时出现未知运行时错误