Javascript 这适用于某些函数,而不适用于其他函数
通常在编写jQuery时,我只使用函数。这次我想给它一些最佳实践,所以我遵循了一个教程。javascript本身似乎是正确的,但我在调用某些函数时遇到了一些问题Javascript 这适用于某些函数,而不适用于其他函数,javascript,jquery,scope,undefined,Javascript,Jquery,Scope,Undefined,通常在编写jQuery时,我只使用函数。这次我想给它一些最佳实践,所以我遵循了一个教程。javascript本身似乎是正确的,但我在调用某些函数时遇到了一些问题 jQuery.noConflict(); (function($j) { 'use strict'; function Site(settings) { this.windowLoaded = false; } Site.prototype = { constructor: Site,
jQuery.noConflict();
(function($j) {
'use strict';
function Site(settings) {
this.windowLoaded = false;
}
Site.prototype = {
constructor: Site,
start: function() {
var me = this;
$j(window).load(function() {
me.windowLoaded = true;
});
this.attach();
},
attach: function() {
this.getPrimaLink();
this.onCloseDialog();
this.triggerDialog();
this.openLink();
},
getPrimaLink: function(){
if($j('#order-data').is(":visible")){
$j( ".content-header" ).append($j( "#findPrimaLink" ));
$j('#findPrimaLink').show();
}
},
onCloseDialog: function(){
$j('#dialog').bind('dialogclose', function(event) {
$j( ".content-header" ).append($j( "#findPrimaLink" ));
$j('#findPrimaLink').show();
});
},
triggerDialog: function(){
$j("[title='Create New Customer']").click(function(){
$j('#findPrimaLink').show();
>>>>> this.openDialog(); <<<<<<
})
},
openLink: function(){
$j('#findPrimaLink').click(function(){
>>> this.openDialog(); <<<<<
});
},
openDialog: function(){
$j( "#dialog" ).dialog({
height: 'auto',
width: 350,
modal: true,
resizable:false,
});
},
};
$j(document).ready(function($j) {
var site = new Site();
site.start();
});
})(jQuery);
jQuery.noConflict();
(职能(j美元){
"严格使用",;
功能站点(设置){
this.windowLoaded=false;
}
Site.prototype={
建造商:现场,
开始:函数(){
var me=这个;
$j(窗口).load(函数(){
me.windowLoaded=true;
});
这个。附加();
},
附件:函数(){
这个.getPrimaLink();
这个.onCloseDialog();
this.triggerDialog();
这是openLink();
},
getPrimaLink:函数(){
如果($j(“#订单数据”)为(“:可见”)){
$j(“.content header”).append($j(“#findPrimaLink”);
$j(“#findPrimaLink”).show();
}
},
onCloseDialog:function(){
$j('#dialog').bind('dialogclose',函数(事件){
$j(“.content header”).append($j(“#findPrimaLink”);
$j(“#findPrimaLink”).show();
});
},
triggerDialog:函数(){
$j(“[title='Create New Customer']”)。单击(函数(){
$j(“#findPrimaLink”).show();
>>>>>this.openDialog();对于您遇到问题的两个函数,您试图在jQuery函数中使用this
,因此this
的作用域是DOM元素,而不是站点
类
要理解为什么会发生这种情况,您应该阅读javascript闭包。和
另外,您的openDialog
函数后面还有一个逗号
p.p.S.值得注意的是,这正是您在start
方法中所做的
var me = this;
$j(window).load(function() {
me.windowLoaded = true;
});
这太好了,非常感谢。我投票并接受了:)
var me = this;
$j(window).load(function() {
me.windowLoaded = true;
});