Jquery 在对象外部工作的函数在对象内部调用时将不工作
当我的页面加载了一个使用mustache的模板时,我正在尝试注入我的页面。如果我调用我的函数来获取DOM,并将此数据注入到 公文包{}对象工作得非常好,HTML被注入页面,数据显示正确。但是,如果我试图将我的函数放在公文包{}对象中并调用该函数,它将不起作用。这对我来说毫无意义,我对此感到非常沮丧。如果有人知道这可能是什么原因以及如何修复,我将非常感激 我能够从portfolio对象中调用数据,它可以工作,因此我知道它与我的数据结构无关 我也没有得到帮助调试的错误 这项工作:Jquery 在对象外部工作的函数在对象内部调用时将不工作,jquery,mustache,Jquery,Mustache,当我的页面加载了一个使用mustache的模板时,我正在尝试注入我的页面。如果我调用我的函数来获取DOM,并将此数据注入到 公文包{}对象工作得非常好,HTML被注入页面,数据显示正确。但是,如果我试图将我的函数放在公文包{}对象中并调用该函数,它将不起作用。这对我来说毫无意义,我对此感到非常沮丧。如果有人知道这可能是什么原因以及如何修复,我将非常感激 我能够从portfolio对象中调用数据,它可以工作,因此我知道它与我的数据结构无关 我也没有得到帮助调试的错误 这项工作: var portf
var portfolio = {
projects: {
"proj": [
{
id:"1",
title:"Heller Recipes",
description:"This web applications was developed to keep track of my dads recipes and make them easily accesible.He is now able to check each user and make a dinner based on what everybody likes or in some cases dont like.",
technologiesUsed:"CodeIgniter, PHP, Sequel Pro, Javascript, jQuery,HTML5, CSS3, SASS, Foundation 5.0",
projectLink:"http://www.google.com",
genre:"web-app",
images: [
{largePic:"img/projects/heller-recipes/thumb.jpg",desktopImg:"img/projects/heller-recipes/desktop.png",desktopMobile:"img/projects/heller-recipes/mobile.png"}
]
},
{
id:"2",
title:"3D Animation",
description:"Created using 4D Cinema Max, a 3d anitmation program that allows you to create realistic renderings and animations.",
technologiesUsed:"CodeIgniter, PHP, Sequel Pro, Javascript, jQuery,HTML5, CSS3, SASS, Foundation 5.0",
projectLink:"http://www.google.com",
genre:"3d",
images: [
{largePic:"img/projects/4dmax.jpg",desktopImg:"img/projects/heller-recipes/desktop.png",desktopMobile:"img/projects/heller-recipes/mobile.png"}
]
},
]
}
};
$('body').on('click', '.logo', function(){
var template = $('#projects_tmp').html();
var html = Mustache.to_html(template, { "proj" : portfolio.projects.proj });
$('.portfolio-wrapper').html(html);
});
这并不是:
var portfolio = {
projects: {
"proj": [
{
id:"1",
title:"Heller Recipes",
description:"This web applications was developed to keep track of my dads recipes and make them easily accesible.He is now able to check each user and make a dinner based on what everybody likes or in some cases dont like.",
technologiesUsed:"CodeIgniter, PHP, Sequel Pro, Javascript, jQuery,HTML5, CSS3, SASS, Foundation 5.0",
projectLink:"http://www.google.com",
genre:"web-app",
images: [
{largePic:"img/projects/heller-recipes/thumb.jpg",desktopImg:"img/projects/heller-recipes/desktop.png",desktopMobile:"img/projects/heller-recipes/mobile.png"}
]
},
{
id:"2",
title:"3D Animation",
description:"Created using 4D Cinema Max, a 3d anitmation program that allows you to create realistic renderings and animations.",
technologiesUsed:"CodeIgniter, PHP, Sequel Pro, Javascript, jQuery,HTML5, CSS3, SASS, Foundation 5.0",
projectLink:"http://www.google.com",
genre:"3d",
images: [
{largePic:"img/projects/4dmax.jpg",desktopImg:"img/projects/heller-recipes/desktop.png",desktopMobile:"img/projects/heller-recipes/mobile.png"}
]
},
]
},
init: function(){
var template = $('#projects_tmp').html();
var html = Mustache.to_html(template, { "proj" : portfolio.projects.proj });
$('.portfolio-wrapper').html(html);
}
}
portfolio.init();
您无法通过名称从
init
中访问portfolio
,因为它还不完全存在。尝试引用别名为this
(即portfolio
)的portfolio
,以防胡须以某种方式撞击this
):
下面是一个简单的例子:
var sampleObject={
物业1:‘你好,世界!’,
init:function(){
var self=这个;
警报(自属性1);
}
}
sampleObject.init()代码>您的第二个示例甚至不是有效的JavaScript。您应该得到控制台错误<代码>公文包.init
看起来像一个对象,但包含过程代码。init
应该是一个函数吗?i、 e.init:function(){…您的代码…}
。如果是,何时调用init
?应该在某个地方发生portfolio.init()
。很抱歉,我不得不为这篇文章剥离我的函数并将其粘贴错误。现在该走了。我这边没有错误。我不知道为什么我在这个问题上会被否决??建议你创建一个简单的演示来复制这个问题。显示的代码如上所述无效,此后我更改了代码。它仍然无效吗?我发布了一篇带有有效代码的新帖子:而且你在任何地方都没有收到任何JS错误?您正在使用浏览器的调试器吗?此代码在什么时候运行(、domready等)?上面的示例代码有效;你的也应该如此。不,我没有收到任何错误。我正在使用chromes webinspector@TravisMichaelHeller:您是指F12工具的“控制台”选项卡,对吗?这就是显示JS错误的地方。正确。我一直使用控制台进行调试。这就是为什么这个问题如此令人沮丧
var portfolio = {
projects: { /* omitted */ },
init: function() {
var self = this;
var template = $('#projects_tmp').html();
var html = Mustache.to_html(template, { "proj" : self.projects.proj });
// ^ use "self"
$('.portfolio-wrapper').html(html);
}
}
portfolio.init();