[英]While creating object and passing in element, getting element is not defined
I am trying to create a js object as below 我正在尝试如下创建一个js对象
$(document).ready(function () {
var stickySidebar = {
element: this.element,
topSpacings: 110,
bottomSpacings: $(document).height() - (element.height() + element.offset().top),
getElement: function () {
return this.element;
},
setElement: function (elm) {
element = elm;
},
makeSideBarSticky: function () {
this.element.sticky({
topSpacing: this.topSpacings,
bottomSpacing: this.bottomSpacings,
zIndex: 2
});
}
}
var stickySidebars = Object.create(stickySidebar );
// var stickySidebars = stickySidebar($('#myElement'));
stickySidebars.setElement($('#myElement'));
stickySidebars.makeSideBarSticky();
});
However, I keep on getting element is not defined. 但是,我继续获取未定义的元素。 I realize element is not defined. 我意识到元素没有定义。 However, I am confused as to how I can pass in the element ($('#myElement')) and then call the function makeSideBarSticky. 但是,对于如何传递元素($('#myElement'))然后调用函数makeSideBarSticky,我感到困惑。 Can someone help me please? 有人能帮助我吗?
You could create an empty div in your HTML document with id="myElement"
, and then modify it via JS. 您可以使用id="myElement"
在HTML文档中创建一个空div,然后通过JS对其进行修改。 This way element is defined for sure 这种方式元素肯定被定义
You are assigning a value to the field. 您正在为该字段分配一个值。 Object will not be created because there is an error while creating the object. 无法创建对象,因为创建对象时出错。 Assign a value of null
instead of this.element
. 分配一个null
代替this.element
。
i dont know what is sticky() is that plugin that you didnt mention you use or something like that , but if we ignored this for a moment , try this : 我不知道sticky()是您没有提到您使用的插件还是类似的插件,但是如果我们暂时忽略了该插件,请尝试以下操作:
$(document).ready(function () {
var stickySidebar = {
element: "",
topSpacings: "",
bottomSpacings: "",
getElement: function () {
return element;
},
setElement: function (elm) {
element = elm;
topSpacings= 110;
bottomSpacings= $(document).height() - (element.height() + element.offset().top);
},
makeSideBarSticky: function () {
element.sticky({
topSpacing: topSpacings,
bottomSpacing: bottomSpacings,
zIndex: 2
});
}
}
var stickySidebars = Object.create(stickySidebar );
// var stickySidebars = stickySidebar($('#myElement'));
stickySidebars.setElement($('#myElement'));
stickySidebars.makeSideBarSticky();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.