0
如果有人能帮助我我有倒闭的问题和检索一个私有变量声明为使用jQuery的ID选择一个元素。我有一个模块,像这样:jQuery的封闭无法检索私有变量与DOM对象
var package = package || {};
(function() {
if(!package.slider)
package.slider = {};
function Slider() {
}
// --------- /Public Members --------- //
Slider.prototype = {
init: function(){
_setEvents();
}
};
// --------- Public Members --------- //
// --------- Private Members --------- //
var shoePreview = $('#shoe-preview'); /*somehow this variable does NOT get recognised even though I have it in the DOM.*/
function _setEvents(){
$.subscribe('getAllPerspectives', function(event){ _getAllPerspectives() });
$.subscribe('showPreview', _show);
$.subscribe('hidePreview', _hide);
}
function _getAllPerspectives(){
_getAngleShoeImage(helper.getCurrentAngle());
$.each(constants.angles, function(key, val){
if(val != helper.getCurrentAngle()){
_getAngleShoeImage(val);
}
});
}
function _getAngleShoeImage(angle){
var shoeImage = shoePreview;
shoeImage.html('<p>Hello World</p>');
}
var that = new Slider();
package.slider = that;
})();
$(document).ready(function(){
$.each(package, function (key, val){
val.init();
});
});
现在,如果你在代码中望去,有一个名为shoePreview变量,我以为不会到模块之外的任何访问。
现在在某处代码以后我会叫一些方法将触发_getAngleShoeImage在那里我有叫shoeImage设置为私有shoePreview变量内部变量。
的问题是,一旦我尝试使用jQuery的HTML()调用它似乎不承认shoePreview变量是什么。
我和代VAR shoeImage = $(“#鞋预览”),然后调用HTML()与所需的标记测试这一点,它工作正常。
这个问题是如此之小,但它的驾驶我坚果。我是否正在编写var shoePreview = $('#shoe-preview');以错误的方式或有更多的东西。任何帮助将不胜感激。
谢谢。
谢谢,我认为这可能做的伎俩。所以在做完这些之后,我能够获得一个对象,但是,当我尝试使用html()来查看内部的标记时,它似乎总是返回undefined,当我尝试将其输出为警报时。这很奇怪。 :(任何可能出错的地方? – koramaiku
你试过我的第一个方法还是最后一个? –
对不起我刚刚意识到你编辑了你的答案:) – koramaiku