欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

E4X 實(shí)例

E4X 使針對(duì) XML 的腳本異常簡(jiǎn)單。

E4X 實(shí)例

作為一個(gè)例子,我們將和一個(gè)表示訂單的 XML 文檔打交道。

XML 文檔類(lèi)似這樣:

<order>
<date>2005-08-01</date>
<customer>
  <firstname>John</firstname>
  <lastname>Johnson</lastname>
</customer>
<item>
  <name>Maxilaku</name>
  <qty>5</qty>
  <price>155.00</price>
</item>
</order>

假如我們把這個(gè) XML 文檔存儲(chǔ)在名為 txt 的字符串中,那么通過(guò)編寫(xiě)下面的 JavaScript 語(yǔ)句,我們就可以把它載入名為 order 的 XML 對(duì)象中:

var order = new XML(txt)

或者我們可以直接使用 XML 文檔對(duì)這個(gè) XML 對(duì)象變量進(jìn)行賦值:

var order = new XML()

order=<order id="555">
<date>2005-08-01</date>
<customer>
  <firstname>John</firstname>
  <lastname>Johnson</lastname>
</customer>
<item>
  <name>Maxilaku</name>
  <qty>5</qty>
  <price>155.00</price>
</item>
</order>

與數(shù)據(jù)打交道:

計(jì)算價(jià)格:

var total=order.item.qty * order.item.price

顯示客戶的全稱(chēng):

document.write(order.customer.lastname)
document.write(",")
document.write(order.customer.firstname)

添加新項(xiàng)目:

order.item+=
<item>
  <name>Pavlova</name>
  <qty>10</qty>
  <price>128.00</price>
</item>

顯示訂單編號(hào):

document.write(order.@id)

如果有多項(xiàng)訂單,則計(jì)算總價(jià):

var price=0
for each (i in order.item)
  {
  price+= i.qty*i.price
  }