本日  昨日
 
03/16(Sun) 02:42

o 文字列中の文字列の記述と文字列の連結
文字列中の文字列の記述
  1. 文字列は「"」(ダブルクォーテーション)で囲みます。
  2. 文字列に対し、+ を行うと、文字列の連結になります。
    "Java"+"Script"は、"JavaScript"になります。
  3. 文字列中の文字列は「'」(シングルクォート)で囲みます。 <IMG SRC="title.gif">というタグをwriteで出力する例: document.write("<IMG SRC='title.gif'>");
文字列の連結
  1. 変数docに文字列として連結し、最後にまとめて出力する例。 document.write("こんにちは"); document.write("「菊池さんちのホームページ」へ"); document.write("ようこそ"); の3つの文字列を var doc="こんにちは" + "「菊池さんちのホームページ」へ" + "ようこそ" document.write(doc,"<BR>"); とまとめて出力することもできます。 (注)関数内でdocument.writeで出力する場合、"<BR>"を出力するか、 document.close()を行わないと結果は表示されませんので注意が必要です。
  2. 文字変数の例。

    当ページのタロット占いで使ってる、占い結果の表示関数での例です。tarotMsgに文字変数(配列)を含む文字列を連結して出力しています。

<SCRIPT language="JavaScript"> <!-- function drawTarot() { var i = Math.floor(Math.random()*tarot.length); var tarotWindow = window.open("","tarotWindow","scrollbars=yes,height=450,width=350") if (tarotWindow != null) { tarotMsg = "<HTML><HEAD><TITLE>タロット占い</TITLE></HEAD>"; tarotMsg += "<BODY BGCOLOR='#000000' TEXT='#FFFF00' LINK='#FFFF00' VLINK='#FFFF00'>"; tarotMsg += "<CENTER><FONT COLOR='deeppink' SIZE=+2>○○さんのタロット占い</FONT>"; tarotMsg += "<div align=right><i>by <a href='http://kikuchisan.net/'"; tarotMsg += " target='_blank'>kikuchisan</a></i></div>"; tarotMsg += "<TABLE BORDER=15><TR><TH><FONT COLOR='#FFBF00' SIZE=+3>" tarotMsg += tarot[i].title + "</FONT><TR><TH><FONT COLOR='white' SIZE=+1>" tarotMsg += tarot[i].direct tarotMsg += "</FONT><TR><TH><IMG SRC=image/" + tarot[i].image + "><TR><TH>"; tarotMsg += tarot[i].contents + "</TABLE><FONT COLOR='lime' SIZE=-1>"; tarotMsg += "占いは繰り返してもよい結果はでません<br>"; tarotMsg += "占いは迷ったときにきっかけを与えるだけです<br>"; tarotMsg += "自分の運命は自分で決めるものです</FONT>"; tarotMsg += "<FORM><INPUT TYPE='button' VALUE='閉じる' onClick='window.close()'>"; tarotMsg += "</FORM></CENTER></BODY></HTML>"; tarotWindow.document.write(tarotMsg); tarotWindow.document.close(); } } // --> </SCRIPT>

 
 
©1997- Kikuchisan's workshop All rights reserved //

スポンサーリンク

関連コンテンツ