document.write()
可用于直接向 HTML 輸出流寫內(nèi)容。簡單的說就是直接在網(wǎng)頁中輸出內(nèi)容。
第一種:輸出內(nèi)容用""括起,直接輸出""號內(nèi)的內(nèi)容。
<script type="text/javascript">
document.write("I love JavaScript!"); //內(nèi)容用""括起來,""里的內(nèi)容直接輸出。
</script>
第二種:通過變量,輸出內(nèi)容
<script type="text/javascript">
var mystr="hello world!";
document.write(mystr); //直接寫變量名,輸出變量存儲的內(nèi)容。
</script>
第三種:輸出多項內(nèi)容,內(nèi)容之間用+號連接。
<script type="text/javascript">
var mystr="hello";
document.write(mystr+"I love JavaScript"); //多項內(nèi)容之間用+號連接
</script>
第四種:輸出HTML標簽,并起作用,標簽使用""括起來。
<script type="text/javascript">
var mystr="hello";
document.write(mystr+"<br>");//輸出hello后,輸出一個換行符
document.write("JavaScript");
</script>
關(guān)于JS輸出空格問題,如果想要實現(xiàn)輸出空格,可以使用特殊字符“ ”實現(xiàn)
<script type="text/javascript">
document.write(" ");//輸出空格
</script>
現(xiàn)在我們來輸出兩條語句,在網(wǎng)頁中輸點內(nèi)容吧!
1. 右邊編輯器第9行,使用document.write輸出mychar變量的內(nèi)容,同時輸出一個換行符。
2. 右邊編輯器第10行,使用document.write一條語句,通過變量mystr,mychar,"的忠實粉絲!",輸出完整的一句"我是JavaScript的忠實粉絲!"。
1.直接輸出內(nèi)容要用""括起來,document.write(mychar+"<br>");
2.多項內(nèi)容同時輸入,用"+"號連接,document.write(mystr+mychar+"的忠實粉絲!");
請驗證,完成請求
由于請求次數(shù)過多,請先驗證,完成再次請求
打開微信掃碼自動綁定
綁定后可得到
使用 Ctrl+D 可將課程添加到書簽
舉報