See the Pen
TestID by 黃昕暐 (@meebox)
on CodePen.
不過在文件中寫到, 到底是哪些名稱會對應到 window 物件的屬性在未來可能會變化, 因此並不建議這樣用, 還是請乖乖的用 document.gettElementById(), 既然如此, 那幹嘛提供這個功能呢?
See the Pen
TestID by 黃昕暐 (@meebox)
on CodePen.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
function remove() {
alert("remove is called.");
}
</script>
</head>
<body>
<button onclick="remove()">刪除</button>
</body>
</html>
按一下『刪除』鈕, 你可能預期會執行程式中的 remove(), 顯示訊息窗, 但實際的結果卻是『刪除』鈕不見了, 這是因為按下『刪除』後執行的是內建的 remove(), 而不是程式中所撰寫的 remove(), 而內建的 remove() 就把『刪除』鈕的 button 元素刪除了。if(typeof window.orientation !== 'undefined'){...}由於一般來說, PC 上的瀏覽器都沒有提供 window.orientation 屬性, 因此可以用來快速判別是否為行動裝置的瀏覽器。window.onload = function(){
var a = navigator.userAgent||navigator.vendor||window.opera;
var level = 60;
if (!(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4)))) {
if (navigator.userAgent.indexOf('Firefox') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Firefox') + 8)) >= 3.6){//Firefox)
document.body.style.MozTransform = "scale(" + level/100 + ")";
document.body.style.MozTransformOrigin = "center top";
}
else {
document.body.style.zoom = "" + level + "%";
document.body.style.msTransformOrigin = "center top";
}
}
};
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<div id="da"></div>
<div id="db"></div>
<div id="dc"></div>
<div id="dd"></div>
<script>
i = 10;
var j = 20;
this.i = 100;
this.j = 200;
$("#da").text(i);
$("#db").text(j);
$("#dc").text(this.i);
$("#dd").text(window.j);
</script>
</body>
</html>
實際輸出的結果就是:100你可以看到不論是用 i 或是 this.i 都是同一份資料, 這是因為 this 指向全域物件, 所以也可以用 window.j 以存取 j, 因為在瀏覽器中, window 就是全域物件。
200
100
200
i = 10;
var j = 20;
this.i = 100;
this.j = 200;
delete this.i;
delete this.j;
$("#da").text(i);
$("#db").text(j);
$("#dc").text(this.i);
$("#dd").text(window.j);
在瀏覽器的 JavaScript console 中就會看到以下的錯誤訊息:Uncaught ReferenceError: i is not defined這是因為 i 並沒有使用 var 宣告, 是全域物件的 configurable 屬性, 因此可以用 delete 刪除, 所以當執行到底下存取變數 i 的值時, i 變數就已經不存在了。
i = 10; var j =20; console.log(global.i); console.log(global.j); console.log(i); console.log(j);執行結果如下:
10透過 node.js 的全域物件 global 可存取到沒有使用 var 宣告的 i 變數, 這是真正全域的變數。可是透過 global 物件嘗試存取 j 變數時, 就會看到 global.j 是未定義的屬性, 這表示 j 並不是真正全域的變數。
undefined
10
20
var tv = require("./testvar.js");
console.log("global==============");
console.log(i);
//console.log(j);
輸出結果如下:10可以看到在程式中可存取到在另一個檔案中宣告的變數 i, 的確是全域變數無誤。若是把程式中最後一行的註解拿掉, 嘗試存取 j, 就會看到:
undefined
10
20
global==============
10
10可以看到 j 並未定義, 這是因為 j 是用 var 在其他檔案中宣告的變數, 不是全域變數, 所以無法存取。
D:\mee\My Box Files\practice\jswebapp\testvar2.js:5
console.log(j);
^
ReferenceError: j is not defined
at Object.(D:\mee\My Box Files\practice\jswebapp\testvar2.js:5:13)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1">
<title>jQuery Mobile Demo</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).bind("mobileinit", function(){
$("[data-role=header],[data-role=footer]").fixedtoolbar({ tapToggle:false });
});
</script>
<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header" data-position="fixed" data-fullscreen="true">
<h1>全螢幕標頭</h1>
</div>
<div data-role="content">
<img src="mycat.jpg" />
</div>
</div>
</body>
</html>
這個程式是希望能在初始化的階段透過第 11 行的程式設定固定在頁首與頁尾的工具列不會因為使用者碰觸畫面而自動隱藏或重現, 不過實際執行之後, 發現設定失效, 工具列還是會以預設的方式自動隱藏或重現。經過查詢 jQuery Mobile 文件以及 Google 爬文後, 發現 mobileinit 事件發生時, 其實後續的 HTML 內容並未載入, 因此事件處理方法中的 $("[data-role=header],[data-role=footer]") 根本就抓不到元素, 自然無法讓期望的設定生效。為了解決這個問題, 只要改成處理 pageinit 事件, 也就是在後續的 HTML 內容生成後再進行設定, 就可以正確執行了。修改的程式如下:$(document).bind("pageinit", function(){
$("[data-role=header],[data-role=footer]").fixedtoolbar({ tapToggle:false });
});
你也可以在事件處理的程式中多加上一行 alert() 呼叫, 就可以觀察實際抓取到的元素內容了:$(document).bind("pageinit", function(){
$("[data-role=header],[data-role=footer]").fixedtoolbar({ tapToggle:false });
alert($("[data-role=header],[data-role=footer]").text());
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>JQuery JSONP</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#useJSONP").click(function(){
$.ajax({
url: 'http://taipeicityopendata.cloudapp.net/v1/TaipeiOGDI/StopWaterInfo',
data: {format: 'json'},
dataType: 'json',
success: jsonpCallback
});
});
});
function jsonpCallback(data){
$('#jsonpResult').text(data.d[0].stitle);
}
</script>
</head>
<body>
<input type="button" id="useJSONP" value="Use JSONP"></input>
<span id="jsonpResult"></span>
</body>
</html>
$("#useJSONP").click(function(){
$.ajax({
url: 'http://taipeicityopendata.cloudapp.net/v1/TaipeiOGDI/StopWaterInfo',
data: {format: 'json'},
dataType: 'jsonp',
success: jsonpCallback
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>JQuery JSONP</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#useJSONP").click(function(){
$.ajax({
url: 'http://taipeicityopendata.cloudapp.net/v1/TaipeiOGDI/StopWaterInfo',
data: {format: 'json'},
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'jsonpCallback',
success: function(){
alert("success");
}
});
});
});
function jsonpCallback(data){
$('#jsonpResult').text(data.d[0].stitle);
}
</script>
</head>
<body>
<input type="button" id="useJSONP" value="Use JSONP"></input>
<span id="jsonpResult"></span>
</body>
</html>