js獲取當前系統是不是win7,相容ie,火狐,谷歌瀏覽器 的阻止事件冒泡
阿新 • • 發佈:2018-12-16
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <!-- 開啟彈窗按鈕 --> <button onclick="test()">獲取當前系統</button> <button class="urlJump" data-type="1" >location.href</button> <button class="urlJump" data-type="2" >top.window.location</button> <div onclick="outdoor()" style="border: 1px solid red;width: 200px;height: 100px" > <p>最外層是一個父類的div</p> <div id="btn"> </div> </div> <script type="text/javascript"> $(function () { $(".urlJump").on("click",function(){ var type=$(this).attr("data-type"); if(type =="" || type == null){ return; } if(type == "1"){ location.href="https://www.baidu.com/"; }else { top.window.location="https://www.iqiyi.com/"; } }); showbtn(); }); function test() { var ua = navigator.userAgent; console.log(ua); if(ua.indexOf("Windows NT 6.1")!=-1) { alert('您使用的是Win7系統!'); } else { alert('不是win7 '); } } function showbtn(){ $("#btn").empty(); var content='<button data-id="hihi" onclick="reload(event,this)">div裡的button</button>'; $("#btn").append(content); } function outdoor(){ location.href="https://www.baidu.com/"; } function reload(event,obj){ stopEvent(event); var bool=window.confirm("確定要取消嗎?"); if(bool){ alert("取消"); var val = $(obj).attr("data-id"); } } function stopEvent(event){ //阻止冒泡事件 //取消事件冒泡 var e=arguments.callee.caller.arguments[0]||event; //若省略此句,下面的e改為event,IE執行可以,但是其他瀏覽器就不相容 if (e && e.stopPropagation) { // this code is for Mozilla and Opera e.stopPropagation(); } else if (window.event) { // this code is for IE window.event.cancelBubble = true; } } </script> </body> </html>