同桌上课用手指进去了好爽_欧美丰满熟妇xxⅹⅹ性大i_成人av天天日天天拍拍_猛男gay帅男gay男男同志_欧美va天堂在线观看_人妻无码av中文系列三里桃花_亚欧免费无码在线观看_久久久精品国产亚洲av水_日韩在线免费看污污污_2021无码专区人妻系列日韩

首頁 優(yōu)化推廣 document.write()和document.writeln()有什么區(qū)別

document.write()和document.writeln()有什么區(qū)別

來源: | 時(shí)間:2013/4/22 19:31:53 |

 

 

document.write()和document.writeln()有什么區(qū)別

一、document.write()和document.writeln()的區(qū)別


解決思路:
    兩者都是JavaScript向客戶端輸出的方法,對比可知寫法上的差別是一個(gè)ln--line的簡寫,換言之,writeln 方法是以行輸出的,相當(dāng)于在 winte 輸出后加上一個(gè)換行符。

 

 

解決思路:
兩者都是JavaScript向客戶端輸出的方法,對比可知寫法上的差別是一個(gè)ln--line的簡寫,換言之,writeln 方法是以行輸出的,相當(dāng)于在?winte?輸出后加上一個(gè)換行符。

注意:document.write方法可以用在兩方面:在網(wǎng)頁載入過程中用實(shí)時(shí)腳本創(chuàng)建網(wǎng)頁內(nèi)容以及用延時(shí)腳本創(chuàng)建本窗口或新窗口的內(nèi)容.該方法需要一個(gè)字符串參數(shù),它是寫到窗口或框架中的HTML內(nèi)容.該字符串參數(shù)可以是變量或值為字符串的表達(dá)式,寫入內(nèi)容常常包含HTML標(biāo)記.
記住,載入網(wǎng)頁后,瀏覽器輸出流將自動關(guān)閉.在些之后任何一個(gè)對當(dāng)前網(wǎng)頁的document.write()方法都將打開一個(gè)新的輸出流,它將清除當(dāng)前網(wǎng)頁輸出內(nèi)容(包括源文檔中的任何變是和值).因此,如果希望用腳本生成的HTML內(nèi)容替換當(dāng)前網(wǎng)頁,就必須把HTML內(nèi)容連接起來賦給一個(gè)變量.這里,使用document.write()來完成寫操作.不必清除文檔并打開一個(gè)新的數(shù)據(jù)流,一個(gè)document.write()調(diào)用就OK了.
關(guān)于document.write()方法,還需要說明它的相關(guān)方法document.close().腳本向窗口(不管是本窗口還是其它窗口)寫完內(nèi)容后必須關(guān)閉輸出流.在腳本的最后一個(gè)document.write() 方法后面.必須確保有document.close()方法.不這樣做就不能顯示圖像和表單.而且,后面調(diào)用的任何document.write() 只會將內(nèi)容追加到網(wǎng)頁后,而不會清除現(xiàn)有內(nèi)容,寫入新值


具體步驟:
1.打開一個(gè)空白窗口。
window.open()
2.用 write 方法向空白窗口寫入代碼。
document.write("Line 1")
document.write("Line 1")
3.用 writeln 方法向空白窗口寫入代碼。
document.writeln("Line 1")
document.writeln("Line 2")
4.完整代碼示例:

with(window.open()){
document.write("Line 1")
document.write("Line 1")
document.writeln("Line 1")
document.writeln("Line 2")
}

注意:兩種方法僅當(dāng)在查看源代碼時(shí)才看得出區(qū)別。
特別提示
把上面的代碼加入網(wǎng)頁中,然后查看彈出窗口的源代碼,將會看到:
Line 1Line 1Line 1
Line 2

頁面效果和源代碼如圖 3.27 所示。

圖 3.27 write和writeln方法的輸出比較

特別說明
總的來說,一般情況下用兩種方法輸出的效果在頁面上是沒有區(qū)別的(除非是輸出到 pre或xmp 元素內(nèi))。

 

 

二、document.write()向指定位置寫html

頁面初始化時(shí)可以正確寫在Select框內(nèi)
但調(diào)用時(shí)就寫在控件外了,不知道document.write()能否想改變innerHTML或outerHTML來動態(tài)寫HTML?以及寫的HTML要用來顯示該如何處理?

如下:

<html>
<head></head>

<script type="text/javascript">
function creatOption(){
for(i=0;i<5;i++)
document.write("<option value='"+i+"'>"+i+"</option>");
}

function openWrite(){
var win=window.open();
win.document.write("Line1");
win.document.write("Line1");
win.document.write("<input type='text' value='1234567890' />");
win.document.writeln("Line1");
win.document.writeln("Line2");
}
</script>

<body>

<Select id="mySelect" name="mySelect">
<script language="javascript">
creatOption();
</script>

</Select>
<input type="button" value="按鈕" onclick="openWrite()"/>
</body>
</html>

服務(wù)熱線

153 8323 9821

功能和特性

價(jià)格和優(yōu)惠

網(wǎng)站和維護(hù)

推廣和優(yōu)化

微信服務(wù)號