怎么用js点击按钮弹出提示框

JavaScript022

怎么用js点击按钮弹出提示框,第1张

需要准备的材料分别有:电脑、浏览器、html编辑器。

1、首先,打开html编辑器,新建html文件,例如:index.html,输入问题基础代码。

2、在index.html中的<body>标签中,输入js代码:

function fun() {

alert('click success!')

}

3、浏览器运行index.html页面,此时点击按钮弹出了提示框。

弹窗代码js怎么改能弹出多个页面代码如下: //至强弹窗代码//容错脚本functionblockError(){returntrue}//当脚本出错时返回真window.onerror=blockErrorif(window.SymRealWinOpen){window.open=SymRealWinOpen}if(window.NS_ActualOpen){window.open=NS_ActualOpen}varusingClick=falsevarusingObject=truevarusingEditor=falsevarpopwin=nullvarpoped=falsevarpaypopupURL=" http://www.baidu.com"if(typeof(contextualAds)=='undefined'){varcontextualAds=''}if(!document.getElementById('paypopupScriptDiv')){document.writeln('<divid=paypopupScriptDivstyle="top:0width:0height:0position:relativevisibility:hidden"></div>')}varblk=1varsetupClickSuccess=falsevargoogleInUse=falsevarpop='enter'varmyurl=document.location.protocol "//" document.location.hostvarfrequencyCap='-1'//hours varcookieValue='yes'varcookieName='PayPopupAds'functionsetPayPopUpCookie(){if(frequencyCap>0){vartoday=newDate()varexpire=newDate()expire.setTime(today.getTime() 3600000*frequencyCap)document.cookie=cookieName "=" escape(cookieValue) "expires=" expire.toGMTString() "path=/"}elseif(frequencyCap==0){document.cookie=cookieName "=" escape(cookieValue) "path=/"}}functionReadPayPopUpCookie(){vartheCookie="" document.cookievarind=theCookie.indexOf(cookieName)if(ind==-1||cookieName=="")return""varind1=theCookie.indexOf('',ind)if(ind1==-1)ind1=theCookie.lengthreturnunescape(theCookie.substring(ind cookieName.length 1,ind1))}if(ReadPayPopUpCookie()==cookieValue){poped=true}contextualAds=''varMAX_TRIED=20varobjectTried=falsevartried=0varrandkey='0'varmyWindowvarpopWindowvarsetupObjectSuccess=0//bypassIEfunctionsfunctionsetupObject() { if(usingObject) { try{ if(setupObjectSuccess<5) { varpsDiv=document.getElementById('paypopupScriptDiv')if(psDiv) { psDiv.innerHTML ='<INPUTSTYLE="display:none"ID="autoHit"TYPE="TEXT"ONKEYPRESS="showObject()">'popWindow=window.createPopup()popWindow.document.body.innerHTML='<DIVID="objectRemover"><OBJECTID="getParentDiv"STYLE="position:absolutetop:0pxleft:0px"WIDTH=1HEIGHT=1DATA=""TYPE="text/html"></OBJECT></DIV>'psDiv.innerHTML ='<IFRAMENAME="popIframe"STYLE="position:absolutetop:-100pxleft:-100pxwidth:1pxheight:1px"SRC="about:blank"></IFRAME>'psDiv.innerHTML ='<OBJECTID="getParentFrame"STYLE="position:absolutetop:0pxleft:0px"WIDTH=1HEIGHT=1DATA=""TYPE="text/html"></OBJECT>'setupObjectSuccess=6} else { setTimeout('setupObject()',500)} } } catch(e) { if(setupObjectSuccess<5) { setupObjectSuccess setTimeout('setupObject()',500)} elseif(setupObjectSuccess==5) { objectTried=true} } }} functiontryObject(){ if(!objectTried

以下代码能实现父窗口关闭子窗口 建a.htm b.htm c.js 三个文件 根据需要改一下就行了 a.htm ---------------------------------------- <html><title>main</title><script language=javascript src="c.js"></script><body>主窗口 <br><br><input type="button" value="b" onclick="window.openSub('b.htm', '__b__', 'width=400,height=300')"><br><br><input type="button" value="关闭窗口" onclick="window.closeSub()"></body></html> b.htm ----------------------------------------------- <html><title>sub11</title><script language=javascript src="c.js"></script><body>子窗口 11 <br><br><input type="button" value="d" onclick="window.openSub('d.htm', '__d__', 'width=400,height=300')"><br><br><input type="button" value="关闭窗口" onclick="window.closeSub()"></body></html> c.js --------------------------------------------- var __winRoot__ = top || parent || window// 祖先窗口对象 var __winParent__ = __winRoot__// 父窗口对象(默认为祖先窗口) var __winTree__ = {subWinNames:''}// 本窗口的子窗口对象集合。 subWinNames 记录所有子窗口的名称,方便遍历 //-------------------------------------------------------- window.closeSub = function() { __winTree_closeAllSubWins__()// 关闭所有子窗口 __winTree_closeWin__()// 关闭当前窗口 } window.openSub = function(url, wname, property) { __winTree_openSubWin__(url, wname, property)} window.onbeforeunload = function() { var n = window.event.screenX - window.screenLeftvar b = n >document.documentElement.scrollWidth-20if(b &&window.event.clientY <0 || window.event.altKey) { window.closeSub()} } //-------------------------------------------------------- ////////////////////////////////////////////////////////// // // 一些函数 // ////////////////////////////////////////////////////////// //-------------------------------------------------------- // 指定窗口是否存在 //-------------------------------------------------------- function __winTree_isExistWin__(hdl){try{var ret=hdl &&!hdl.closed}catch(e){var ret=false}return ret} //-------------------------------------------------------- ////////////////////////////////////////////////////////// // // 窗口控制 // ////////////////////////////////////////////////////////// //-------------------------------------------------------- // 构造窗口缺省名字 //-------------------------------------------------------- function __winTree_getDefaultWinName__() { return "__wn" + (new Date().getTime()) + "__"// 用当前时间构造窗口缺省名字 } //-------------------------------------------------------- // 打开子窗口,并添加到窗口目录树 //-------------------------------------------------------- function __winTree_openSubWin__(url, wname, property) { var hdl = window.open(url, wname, property)__winTree_addSubWin__(wname, hdl)// 保存到子窗口对象集合 } //-------------------------------------------------------- // 保存到子窗口对象集合 //-------------------------------------------------------- function __winTree_addSubWin__(subWinName, sWin) { var win = top ? top : window// 获取本窗口的最上层窗口 var winTree = win.__winTree__winTree[subWinName] = sWin// 将子窗口对象加入到集合中 winTree["subWinNames"] += subWinName + ","// 所有子窗口名称集合 } //-------------------------------------------------------- // 将指定窗口的子孙窗口对象,添加到指定窗口的父窗口中 //-------------------------------------------------------- // 使指定窗口的子孙窗口保持在目录树中的结构 // 否则指定窗口的子孙窗口将从目录树结构中断开,不能统一控制。 // (用于关闭指定窗口的操作时) //-------------------------------------------------------- function __winTree_addSubWins2pWin__(thisWin) { thisWin = thisWin || window// 缺省为本窗口 var pWin = thisWin.__winParent__var winTree = thisWin.__winTree__var arrSubWinNames = winTree.subWinNames.split(",")// 子窗口名字数组 var intSubWinNamesLen = arrSubWinNames.lengthfor(var i=0i<intSubWinNamesLeni++) { var subWinName = arrSubWinNames[i]// 指定窗口的子孙窗口名字 if (!subWinName) continuevar sWin = winTree[subWinName]// 指定窗口的子孙窗口对象 // 为避免和父窗口中已有的子窗口的名称相同,因此要加上缺省的、以当前时间来构造的名称 pWin.__winTree_addSubWin__(subWinName + __winTree_getDefaultWinName__(), sWin)} } //-------------------------------------------------------- // 本窗口的所有下级子窗口执行指定动作 //-------------------------------------------------------- // funcName 要执行的函数名称(例如 close ,即 window.close() ,或者其他自己定义的函数,例如 changeBgColor 等等) // argVals 要执行的函数的参数值 //-------------------------------------------------------- function __winTree_doWithSubs__(win, funcName, argVals) { if (!win || !funcName) returnvar winTree = win.__winTree__var arrSubWinNames = winTree.subWinNames.split(",")// 子窗口名字数组 var intSubWinNamesLen = arrSubWinNames.lengthfor(var i=0i<intSubWinNamesLeni++) { var subWinName = arrSubWinNames[i]// 子窗口名字 if (!subWinName) continuevar sWin = winTree[subWinName]// 子窗口对象 if (__winTree_isExistWin__(sWin)) { sWin.__winTree_doWithSubs__(sWin, funcName, argVals)// 查找该子窗口的下级子窗口 sWin[funcName](argVals)// 执行指定的动作 } } } //-------------------------------------------------------- // 初始化窗口目录树 //-------------------------------------------------------- function __winTree_init__() { if (__winTree_isExistWin__(opener)) // 如果存在父窗口 { var pWin = opener.top// 父窗口。有可能是在 iframe 中打开本窗体,因此父窗体应该是 opener 的顶级窗体 __winParent__ = pWin// 祖先窗口 __winRoot__ = pWinvar ppWin = pWin.openerif (__winTree_isExistWin__(ppWin)) // 修正祖先窗口 { __winRoot__ = pWin.__winRoot__} } //alert([__winRoot__.document.title, __winParent__.document.title, window.document.title])// for test only } //-------------------------------------------------------- // 关闭当前窗口 //-------------------------------------------------------- function __winTree_closeWin__(win) { win = win || window// 缺省为关闭当前窗口 var pWin = win.__winParent__if (__winTree_isExistWin__(pWin)) // 如果指定窗口有父窗口,且未关闭 { __winTree_addSubWins2pWin__(win)// 将指定窗口的子孙窗口对象,添加到指定窗口的父窗口中 } win.opener = null// 此语句用于取消“是否关闭此窗口”的提示 win.close()// 关闭指定窗口 pWin.focus()// 聚焦到父窗口 } //-------------------------------------------------------- // 关闭本窗口的所有下级子窗口 //-------------------------------------------------------- function __winTree_closeAllSubWins__(win) { win = win || window// 缺省为关闭当前窗口 __winTree_doWithSubs__(win, "close")} //-------------------------------------------------------- // 初始化 //-------------------------------------------------------- __winTree_init__()