将jquery与oojavascript结合使用
我正在尝试创建一个jquery弹出脚本OO风格。我这样做是因为我想用更多的jquery/javascript扩展这段代码,而不会失去监督。我收到的错误是将jquery与oojavascript结合使用,javascript,jquery,oop,Javascript,Jquery,Oop,我正在尝试创建一个jquery弹出脚本OO风格。我这样做是因为我想用更多的jquery/javascript扩展这段代码,而不会失去监督。我收到的错误是Object#没有方法“centerPopup”和资源被解释为脚本,但使用MIME类型text/x-c进行传输:我不熟悉OO javascript,但对OO PHP有相当的经验 function popup(){ var popupStatus = 0; $(document).ready(function () {
Object#没有方法“centerPopup”
和资源被解释为脚本,但使用MIME类型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()
x
这不是你真正想的。它不是弹出窗口的实例,而是DOM元素(#button
)。您可以通过在类的开头保存实例上的引用来解决此问题:
function popup(){
var self = this;
this.popupStatus = 0; // you should use `this` here
$(document).ready(function () {
$("#button").click(function()
{
self.centerPopup();
self.loadPopup();
});
/* ... snip ... */
可能重复的
$("#button").click(function()
{
this.centerPopup();
this.loadPopup();
});
function popup(){
var self = this;
this.popupStatus = 0; // you should use `this` here
$(document).ready(function () {
$("#button").click(function()
{
self.centerPopup();
self.loadPopup();
});
/* ... snip ... */