Javascript错误
我得到一个错误“弹出未定义”有人知道为什么吗 这是我的密码 编辑: 编辑:Javascript错误,javascript,Javascript,我得到一个错误“弹出未定义”有人知道为什么吗 这是我的密码 编辑: 编辑: var popupInstance=null; 函数createCrossBrowserPopup(){ 警报(“创建”); 返回新的_popup(); } 函数_popup(){ this.wnd=null; this.hiddenFrame=null;//IE 6的变通方法-使用div和select(div出现在select元素后面) this.showHiddenFrame=showHiddenFrame; th
var popupInstance=null;
函数createCrossBrowserPopup(){
警报(“创建”);
返回新的_popup();
}
函数_popup(){
this.wnd=null;
this.hiddenFrame=null;//IE 6的变通方法-使用div和select(div出现在select元素后面)
this.showHiddenFrame=showHiddenFrame;
this.displayPopupContainer=displayPopupContainer;
this.html=“”;
this.setHTML=_setHTML;
this.show=\u show;
this.hide=\u hide;
this.triggerElementId=null;
this.blur=null;
}
函数_setHTML(html,事件){
this.html=html;
如果(this.wnd!=null){
this.wnd.html(this.html);
}
如果(事件!=null)
stopEventPropagation(事件,null);
}
///leftOffset:弹出窗口的左偏移量
///topOffset:弹出窗口的顶部偏移
///宽度:弹出窗口的宽度
///高度:弹出窗口的高度
///元素:导致弹出窗口打开的元素
///事件:打开弹出窗口的事件
函数显示(左偏移、地形偏移、宽度、高度、元素、事件){
//隐藏以前的弹出窗口(如果存在)
if(popupInstance!=null&&element.id!=popupInstance.triggerElementId){
popupInstance.hide();
返回;
}
//如果popupInstance!=null,这意味着调用了相同的弹出窗口,再次创建它没有意义
if(popupInstance==null){
leftOffset=leftOffset!=null?leftOffset:0;
topOffset=topOffset!=null?topOffset:0;
displayPopupContainer(元素、宽度、高度、leftOffset、topOffset);
//IE 6的解决方法-使用div和select(div出现在select元素后面)
this.showHiddenFrame(元素、宽度、高度、左偏移、拓扑偏移);
//工作区结束
setElementsPosition(元素、左偏移、拓扑偏移);
//保存触发弹出窗口的元素的id onblur事件
//并保存此对象的实例-为$(文档)。单击并$(窗口)。调整大小
this.triggerElementId=element.id;
this.blur=element.onblur;
element.onblur=null;
popupInstance=this;
}
停止事件传播(事件);
}
函数_hide(){
$(“.modal_popup_container”).hide();
如果(this.triggerElementId!=null){
//分配回blur事件(使用简单的Javascript,而不是jQuery,因为它会导致onblur被触发2次)并重置一些变量
$('#'+this.triggerElementId)[0].onblur=this.blur;
if($.isFunction(this.blur)){
$('#'+this.triggerElementId)[0].onblur();
}
其他的
if(typeof(this.blur)='string')
{
var regex=new RegExp('(?=[\w.])this(?!\w)/g';
eval(this.blur.replace(regex,'$(\'\'\'.'+this.triggerElementId+'\')[0]');
}
//重置变量
this.triggerElementId=null;
this.blur=null;
popupInstance=null;
}
}
函数displayPopupContainer(元素、宽度、高度、leftOffset、topOffset){
if(this.wnd==null){
this.wnd=$('');
this.wnd.addClass('modal_popup_container');
this.wnd.css({
'位置':'绝对',
“z索引”:“999”,
'边距':'0'});
}
css({'width':width,'height':height});
this.wnd.html(this.html);
this.wnd.show();
var parentElement=$(element.parent();
$(parentElement).append(this.wnd);
}
函数showHiddenFrame(元素、宽度、高度、左偏移、拓扑偏移){
if(this.hiddenFrame==null){
this.hiddenFrame=$('');
this.hiddenFrame.addClass('modal_popup_container');
this.hiddenFrame.attr({
“src”:“javascript:'html/html';”,
“滚动”:“否”,
“帧边框”:0
});
this.hiddenFrame.css({
“位置”:“绝对”,
“边界”:“无”,
“显示”:“块”,
“z指数”:“998”,
“边距”:“0”
});
}
css({“width”:width,“height”:height});
this.hiddenFrame.show();
var parentElement=$(element.parent();
$(parentElement).append(this.hiddenFrame);
}
函数setElementsPosition(元素、左偏移、拓扑偏移){
$(“.modal\u popup\u container”)。位置({
我的:“左上”,
在“左下角”,
其中:$(元素),
偏移量:leftOffset+“”+topOffset,
碰撞:“适合”
});
}
函数stopEventPropagation(事件)
{
//管理FireFox和IE事件,检查事件是否已通过,否则请尝试获取window.event
var e=(!event)?window.event:event;
//避免当前单击事件向上传播
if(类型(e)!=“未定义”){
如果(如停止播放)
e、 停止传播();
否则e.cancelBubble=true;
}
}
//当用户在弹出窗口外单击时隐藏弹出窗口
$(文档)。单击(
功能(事件){
if($(event.target).closest('.modal\u popup\u container').get(0)=null
&&popupInstance!=null){
popupInstance.hide();
}
});
$(窗口)。调整大小(
功能(事件){
if(popupInstance!=null){
popupInstance.hide();
}
});
var popup=createCrowserPopup();
setHTML(“测试”,空);
函数changePopupHTML(){
setHTML(“test222”,null);
}
第二次编辑:在最新版本中,问题是您在javascript框架中输入的javascript出现在head标记中。然后引用body标记中的变量。这项工作:
您的JSFIDLE已全部顶起。可能需要再次查看,并正确设置。你也有不少的缺点
<html>
<head>
</head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.js"></script>
<script type="text/javascript">
var popupInstance = null;
function createCrossBrowserPopup(){
alert("creating");
return new _popup();
}
function _popup() {
this.wnd = null;
this.hiddenFrame = null; //workaround for IE 6 - with divs and select (divs are appearing behind select elements)
this.showHiddenFrame = showHiddenFrame;
this.displayPopupContainer = displayPopupContainer;
this.html = "";
this.setHTML = _setHTML;
this.show = _show;
this.hide = _hide;
this.triggerElementId = null;
this.blur = null;
}
function _setHTML(html, event) {
this.html = html;
if (this.wnd != null) {
this.wnd.html(this.html);
}
if (event != null)
stopEventPropagation(event, null);
}
/// leftOffset: the left offset of the popup
/// topOffset: the top offset of the popup
/// width: the width of the popup
/// height: the height of the popup
/// element: the element which causes the popup to be opened
/// event: the event which cased the popup to be opened
function _show(leftOffset, topOffset, width, height, element, event) {
// Hide previous popup if existed
if (popupInstance != null && element.id != popupInstance.triggerElementId) {
popupInstance.hide();
return;
}
// if popupInstance != null this means that the same popup was called, makes no sense to create it again
if (popupInstance == null){
leftOffset = leftOffset != null ? leftOffset : 0;
topOffset = topOffset != null ? topOffset : 0;
this.displayPopupContainer(element, width, height, leftOffset, topOffset);
//workaround for IE 6 - with divs and select (divs are appearing behind select elements)
this.showHiddenFrame(element, width, height, leftOffset, topOffset);
// end of workaround
setElementsPosition(element, leftOffset, topOffset);
// save the id, onblur event of the element that fires the popup
// and also save the instance of this object - for $(document).click and $(window).resize
this.triggerElementId = element.id;
this.blur = element.onblur;
element.onblur = null;
popupInstance = this;
}
stopEventPropagation(event);
}
function _hide() {
$(".modal_popup_container").hide();
if (this.triggerElementId != null) {
// Assign back the blur event (using simple Javascript, not jQuery as it is causing onblur to be fired 2 times) and reset some variables
$('#' + this.triggerElementId)[0].onblur = this.blur;
if ($.isFunction(this.blur)) {
$('#' + this.triggerElementId)[0].onblur();
}
else
if(typeof(this.blur) == 'string')
{
var regex = new RegExp('(?=[\w.])this(?!\w)/g');
eval(this.blur.replace(regex, '$(\'#' + this.triggerElementId + '\')[0]'));
}
// reset variables
this.triggerElementId = null;
this.blur = null;
popupInstance = null;
}
}
function displayPopupContainer(element, width, height, leftOffset, topOffset) {
if (this.wnd == null) {
this.wnd = $('<div />');
this.wnd.addClass('modal_popup_container');
this.wnd.css({
'position' : 'absolute',
'z-index' : '999',
'margin' : '0'});
}
this.wnd.css({ 'width': width, 'height': height });
this.wnd.html(this.html);
this.wnd.show();
var parentElement = $(element).parent();
$(parentElement).append(this.wnd);
}
function showHiddenFrame(element, width, height, leftOffset, topOffset) {
if (this.hiddenFrame == null) {
this.hiddenFrame = $('<iframe />');
this.hiddenFrame.addClass('modal_popup_container');
this.hiddenFrame.attr({
"src": "javascript:'<html></html>';",
"scrolling": "no",
"frameborder": 0
});
this.hiddenFrame.css({
"position": "absolute",
"border": "none",
"display": "block",
"z-index": "998",
"margin": "0"
});
}
this.hiddenFrame.css({ "width": width, "height": height });
this.hiddenFrame.show();
var parentElement = $(element).parent();
$(parentElement).append(this.hiddenFrame);
}
function setElementsPosition(element, leftOffset, topOffset) {
$(".modal_popup_container").position({
my: "left top",
at: "left bottom",
of: $(element),
offset: leftOffset + " " + topOffset,
collision: "fit"
});
}
function stopEventPropagation(event)
{
// manage FireFox and IE events, check if event was passed, else try and get window.event
var e = (!event) ? window.event : event;
// avoid that the current click event propagates up
if (typeof (e) != "undefined") {
if (e.stopPropagation)
e.stopPropagation();
else e.cancelBubble = true;
}
}
// hide the popup when user clicks outside it
$(document).click(
function(event) {
if ($(event.target).closest('.modal_popup_container').get(0) == null
&& popupInstance != null) {
popupInstance.hide();
}
});
$(window).resize(
function(event) {
if (popupInstance != null) {
popupInstance.hide();
}
});
</script>
<script language="javascript" type="text/javascript">
var popup = createCrossBrowserPopup();
popup.setHTML("<div style='width:100%; height:100%;
background-color:#AAFFEE;border-width: 2px; border-color: silver; border-style:
solid;' onclick='changePopupHTML();'> test </div>", null);
function changePopupHTML() {
popup.setHTML("<div style='width:100%; height:100%; background-color:#AA11EE;border-width: 2px; border-color: silver; border-style: solid;'> test222 </div>", null);
}
</script
<body>
<input type="text" id="txtExample" onclick="popup.show(25, 0, 100, 50, this, event);" />
</body>
</html>