简体   繁体   English

这适用于某些功能而不是其他功能

[英]this works on some functions and not others

Normally when writing jQuery i just use functions. 通常,在编写jQuery时,我只是使用函数。 This time I want to give it a little sprinkle of best practice and so I followed a tutorial. 这次我想给它一些最佳实践,所以我遵循了一个教程。 The javascript itself seems to be correct but I am having a few problems calling certain functions. javascript本身似乎是正确的,但是调用某些函数时遇到一些问题。

 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); 

Within the start and attach function I am able to call each function by placing 'this' in front of it. 在start和attach函数中,我可以通过在其前面放置“ this”来调用每个函数。 But when I try to call openDialog() from openLink() and triggerDialog() I get - Uncaught TypeError: undefined is not a function. 但是,当我尝试从openLink()和triggerDialog()调用openDialog()时,我得到了-未被捕获的TypeError:undefined不是一个函数。

Why is this and what should I do to fix it? 为什么会这样,我应该怎么做才能解决?

For both functions you're having a problem with, you're trying to use this inside of a jQuery function, so this 's scope is to the DOM element, not the Site class . 对于您遇到的两个函数,您都试图在jQuery函数中使用this ,因此this的作用域是DOM元素,而不是Site

triggerDialog: function(){
    var site = this;

    $j("[title='Create New Customer']").click(function(){
        $j('#findPrimaLink').show();
        site.openDialog();
        console.log(this); //remove this for production, but you can see that `this` points to a DOM element
    })
},

openLink: function(){
    var site = this;

    $j('#findPrimaLink').click(function(){
        site.openDialog();
    });
},

To understand why this happens, you should read about javascript Closures. 要了解发生这种情况的原因,您应该阅读有关javascript闭包的信息。 Here and here . 在这里这里

PS you have an extra comma after your openDialog function. PS,您的openDialog函数后还有一个逗号。

PPS It's also worth noting that this is exactly what're you're doing inside the start method. PPS还值得注意的是,这正是您在start方法中正在执行的操作。

var me = this;

$j(window).load(function() {
    me.windowLoaded = true;
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 getJSON适用于某些地址,但不适用于其他地址 - getJSON works for some addresses, but not others Fetch 在某些情况下有效,但在其他情况下无效 - Fetch works in some circumstances but not in others javascript在某些浏览器中有效,而在其他浏览器中则取决于环境 - javascript works in some browsers not others depending on environment GAS withSuccessHandler用于某些功能,但不适用于其他功能 - GAS withSuccessHandler working for some functions but not others 如何将函数添加到某些jQuery对象,而不是其他jQuery对象? - How to add functions to some jQuery objects, but not others? 为什么某些函数是全局的而其他函数是对象的一部分? - Why are some functions global and others part of an object? Chrome壁纸API适用于某些图片,不适用于其他图片,没有明显的原因 - Chrome wallpaper API works with some images, not with others, no apparent reason 设置&#39;display&#39;属性的Javascript函数在某些情况下有效,而在其他情况下无效 - Javascript function to set 'display' attribute works in some situations and not others jQuery scrollTop()按增量自动滚动在某些网站上有效,但在其他网站上则无效。 为什么? - jQuery scrollTop() autoscroll by increment works on some sites, but not others. Why? jQuery Validation Plugin适用于某些规则,但忽略其他规则 - jQuery Validation Plugin works for some rules but ignores others
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM