简体   繁体   English

使用带有OO Javascript的jquery

[英]Using jquery with OO Javascript

I'm trying to çreate a jquery popup script OO style. 我正在尝试使用jquery弹出脚本OO样式。 I'm doing this because I would like to expand this code with more jquery/javascript without losing oversight. 我这样做是因为我想用更多的jquery / javascript扩展这段代码,而不会失去监督。 The errors I'm receiving are Object #<HTMLDivElement> has no method 'centerPopup' and Resource interpreted as Script but transferred with MIME type text/xc: I'm new to OO javascript, but have quite the experience in OO PHP 我收到的错误是Object #<HTMLDivElement> has no method 'centerPopup'Resource interpreted as Script but transferred with MIME type text/xc:我是OO javascript的新手,但在OO PHP中有相当丰富的经验

function popup(){

    var popupStatus = 0;

    $(document).ready(function () {
        $("#button").click(function()
        {
            this.centerPopup();
            this.loadPopup();
        });

        $("#backgroundPopup").click(function()
        {
            this.disablePopup();
        });

        $(document).keypress(function(e)
        {
            if(e.keyCode==27 && popupStatus==1)
            {
                this.disablePopup();
            }
        });

    });

      this.loadPopup = function (){
        if(this.popupStatus==0)
        {
            $("#backgroundPopup").css(
            {
                "opacity": "0.7"
            });
            $("#backgroundPopup").fadeIn("slow");
            $("#popupContact").fadeIn("slow");

        this.popupStatus = 1;
        }
    }

    this.disablePopup = function (){
        if(this.popupStatus==1)
        {
            $("#backgroundPopup").fadeOut("slow");
            $("#popupContact").fadeOut("slow");
            this.popupStatus = 0;
        }
    }

    this.centerPopup = function (){
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        var popupHeight = $("#popupContact").height();
        var popupWidth = $("#popupContact").width();

        $("#popupContact").css(
        {
            "position": "absolute",
            "top": windowHeight/2-popupHeight/2,
            "left": windowWidth/2-popupWidth/2
        });

        $("#backgroundPopup").css(
        {
            "height": windowHeight
        });
    }
}

var popup = new popup()


<!DOCTYPE HTML>

<html>
<head>
<link rel="stylesheet" href="css/popup.css" type="text/css" media="screen" />
<script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript"></script>
<script src="js/popup2.js" type="text/javascript"></script>
</head>
<body>

    <center>
        <div id="button"><input type="submit" value="Popup!" /></div>
    </center>

    <div id="popupContact">
        <a id="popupContactClose">x</a>
    </div>

    <div id="backgroundPopup"></div>

</body>
</html>
 $("#button").click(function()
        {
            this.centerPopup();
            this.loadPopup();
        });

this isn't what you actually think. this不是你的想法。 It's not the instance of popup , but the DOM element ( #button ). 它不是popup的实例,而是DOM元素( #button )。 You can fix this by saving a reference on your instance at the beginning of your class: 您可以通过在类的开头保存实例的引用来解决此问题:

function popup(){
    var self = this;
    this.popupStatus = 0; // you should use `this` here

    $(document).ready(function () {
        $("#button").click(function()
        {
            self.centerPopup();
            self.loadPopup();
        });
/* ... snip ... */

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM