TypeError:$(..).SetHeight不是函数
TypeError: $(...).SetHeight is not a function
我正在为我的网站编写一个流畅的布局,我使用jQuery来设置div的高度和宽度。在SetHeight函数中,我将所需高度作为函数的第一个参数,然后减去边距并应用新高度。
function SetHeight(height){
var outer = $(this).outerHeight(true);
var current = $(this).height();
var margin = o-c;
height = height-margin;
$(this).css("height",height);
};
以下是触发错误消息的代码:
var h = $(window).height();
$("#Slider").SetHeight(h-260).removeClass("mobile");
我正在使用$(文档).ready和美元(窗口).resize与计时器一起检查是否需要调整任何DIV.的大小
我错过了什么?
您已经定义了一个函数,但使用它就像使用jQuery对象上的方法一样。
如果你这样做:
function f(param1, param2){
// code
}
你需要这样使用它:
f(div, 4);
如果你想像现在这样使用它,你需要做这样的事情:
$.fn.SetHeight = function(param1, param2){
// code
}
所以你可能想要这个:
function SetHeight(height){
var outer = this.outerHeight(true);
var current = this.height();
var margin = outer-current;
height = height-margin;
this.css("height",height);
return this; // to allow chaining
};
$.fn.SetHeight = SetHeight; // add as a jQuery method
然后你可以使用你的代码:
$("#Slider").SetHeight(h-260).removeClass("mobile");
你可以像在你的例子中那样使用它。
注意,this
已经是一个jQuery对象,所以不需要像:$(this)
那样包装它。您应该在末尾返回this
以支持链接,就像在末尾调用.removeClass("mobile")
一样。
另请参阅:http://api.jquery.com/jQuery.fn.extend/
您看到这个错误是因为没有一个Jquery object
具有像SetHeight()
这样的函数。您应该像下面这样更改函数定义和函数调用,以满足您的需求。
试试,
function SetHeight(xElement,height){
var outer = xElement.outerHeight(true);
var current = xElement.height();
var margin = o-c;
height = height-margin;
xElement.css("height",height);
return xElement;
};
功能调用,
var h = $(window).height();
SetHeight($("#Slider"),h-260).removeClass("mobile");
SetHeight
函数不是jQuery对象原型的一部分。
让它成为一个,或者让它以jQuery或DOM对象作为第一个参数。
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- JQuery合并了keyup和focusout两个函数
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 我可以在json对象中添加一个函数吗
- AngularJS:我可以跳过函数参数回调吗
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- jquery点击函数select&取消选择
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- Node.js v6.2.0类扩展不是函数错误
- 比较从函数和生成的日期对象
- jQuery中是否内置了任何字符串格式化函数
- TypeError:$(..).SetHeight不是函数