獲取文本信息的方法有很多,本例使用 jQuery 的 text 方法,簡單實用。獲取後再用 alert 把文本彈出,代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
p { margin:8px; font-size:20px; color:blue; cursor:pointer; }
b { text-decoration:underline; }
button { cursor:pointer; }
</style>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
</head>
<body>
<b>Show My Text</b>
<div id="TEXT"></div>
<script>
$("#TEXT").load("qqq.txt", function(response, status, xhr) {
var text=$(this).text();
alert( text);
});
</script>
</body>
</html>這段代碼先通過 load 把 qqq.txt 的內容加載到 #TEXT 元素,再用 text() 取出其中的文本並彈出顯示。