gpt4 book ai didi

javascript - 将 jquery 与 OO Javascript 一起使用

转载 作者:行者123 更新时间:2023-12-02 18:50:36 24 4
gpt4 key购买 nike

我正在尝试创建一个 OO 风格的 jquery 弹出脚本。我这样做是因为我想在不失去监督的情况下使用更多 jquery/javascript 扩展此代码。我收到的错误是 Object #<HTMLDivElement> has no method 'centerPopup'Resource interpreted as Script but transferred with MIME type text/x-c:我是 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();
});

并不是您真正的想法。它不是 popup 的实例,而是 DOM 元素 (#button)。您可以通过在类(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 ... */

关于javascript - 将 jquery 与 OO Javascript 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15865560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com