顯示具有 JavaScript 標籤的文章。 顯示所有文章
顯示具有 JavaScript 標籤的文章。 顯示所有文章

2008/01/01

[教學]HTML文件的階層架構 parentElement,children,contain

parentElement() 顯示比myP大一層的標籤
children()    顯示再往下一層找
contains():           檢查是否有子標籤(傳回true or flase)


source:


<html>
<head>
<title>存取父元素和子元素</title>
<script language="JavaScript">
function showParentTag(){
   alert(myP.parentElement.tagName);
}
function showChildsTag(){
   var strTags = "";
   for (var i=0; i<myP.children.length; i++){
      strTags += myP.children.item(i).tagName + " ";
   }
   alert(strTags);
}
function checkChild(){
   var objEle = document.all("myP").children.item(1);
   if (document.all("myP").contains(objEle))
      alert("myP有子元素" + objEle.tagName);
   else
      alert("myP沒有子元素" + objEle.tagName);
}
</script>
</head>
<body id="myBody">
<h2 id="myH2">存取父元素和子元素</h2>
<hr>
<p id="myP">存取<b><i>HTML</i></b>文件的<b>父元素</b>和<i>子元素</i></p>
<form>
<input type="button" onclick="showParentTag()" value="取得父元素">
<input type="button" onclick="showChildsTag()" value="顯示子元素">
<input type="button" onclick="checkChild()" value="檢查是否有子元素">
</form>
</body>
</html>


[教學]利用id取得HTML標籤

選擇ID寫法
   document.all ["ID"]
   document.all ("ID")

選擇索引值
   document.all["num"]
   document.all("num")


example:

<script type="text/javascript"><!--
function show(){
    document.write(document.all["mybody"].tagName);
}
--></script>

也可改寫成    document.write(mybody.tagName);  直接指定id

[教學]取得HTML文件裡的標籤和索引值


document.all(Num).tagName - 取得標籤名稱
document.all(Num).sourceIndex  - 取得索引值

for(i=0; i<document.all.length; i++){
        document.write(document.all(i).sourceIndex+" "+document.all(i).tagName)
}

[教學]取得檔案更新時間

document.write(document.lastModified);

[教學]取得網頁文件資訊

document.title 網頁標題
document.URL 本頁URL
document.referrer 上一頁URL

document.write(document.title);
document.write(document.URL);
document.write(document.referrer);

[教學]輸出在網頁上

1. document.write("String")
這三種寫法結果都一樣,都輸出粗體字

寫法一
<script type="text/javascript">
<!--
    document.write("<b>寫法一</b>");
//-->
</script>


寫法二
<b>
<script type="text/javascript">
<!--
    document.write("寫法二");
//-->
</script>
</b>


寫法三
<script type="text/javascript">
<!--
    document.write("寫法三".bold());
//-->
</script>

Tips

要以 document.write() 輸出大量文字時,為避免部分Browser version error
最好用 document.write("字串A"+"字串B"),將文章分割。

[教學]物件、函數、變數命名規則

1. 名稱須為以大小寫 的英文字母或從底線"_"開始的字串。
2. 不能使用中文。
3. 不能使用空白、逗號、問號、引號。
4. 字串可含有數值,但不能放在最前面。
5. 大小寫有區別。
6. 不能使用保留字。

系統預先保留起來的單字稱為保留字,這些保留字都不能拿來做任何物件命名
保留字


abatract
catch
default
extends
for
import
long
private
static
throw
typeof
boolean
char
delete
false
function
in
native
protected
super
throws
var
break
class
do
final
goto
instanceof
new
public
switch
transient
void
byte
const
double
finally
if
int
null
return
synchronized
true
while
case
contuinue
else
float
implements
interface
package
short
this
try
with


2007/12/31

[教學]JavaScript外部呼叫方法

URL.js是javasctipt路徑
<script src="URL.js" type="text/javascript"><!--

--></script>

[教學]JavaScript note&Tips

JavaScript語法撰寫與註解
JavaScript外部呼叫方法
物件、函數、變數命名規則

document Object
輸出在網頁上
取得網頁文件資訊
取得檔案更新時間
取得HTML文件裡標籤的id
利用id取得HTML標籤
HTML文件的階層架構 parentElement, children, contain

[教學]JavaScript語法撰寫與註解

1. 建議寫法
<script type="text/javascript">
<!--
    //JavaScript code
//-->
</script>


2. HTML4.0之後不建議使用

<script language="javascript">
<!--
    //JavaScript code
//-->
</script>


3. 這樣寫會造成在script裡的都解讀成JavaScript
<script>
<!-- 
    //JavaScript code
//-->
</script>

4. 註解寫法

<script type="text/javascript">
<!--
    //這是單行註解
    /*
    這是多行註解
    */
//-->
</script>