没有公告
自学动态Html---教程四
请点击下边的文字……
下面我们来设计一个有趣的动态页面。 例13 文本的动态输入与输出 <html> <head> <title>DHtml举例13</title> <style><!-- body {font-family:"宋体";color="blue";font-size:"9pt"} .blue {color:blue;font-size:9pt} --> </style> <script language="javascript"> function OutputText() { if(frm.txt.text!="") { Output.innerHTML="在此处输出文本:<u>"+frm.txt.value+"</u>";} //Output为一对象。 else { Output.innerText="在此处输出文本:";} }//function </script> </head> <body> <p><br></p> <form name="frm"> <p><font color="gray">请在文本框中输入文字:</font> <input type="text" name="txt" size="50"><br> <input type="button" value="输出文本" name="B1" class="blue" onclick="OutputText()"></p> </form> <p id="Output">在此处输出文本:</p> </body> </html> 此例的动态效果如下,先在文本框中输入文本,然后按“输出文本”的按钮,接着网页便会自动输出您所输入的文本。
请在文本框中输入文字:
在此处输出文本:
此外,我们还可使用insertAdjacentHTML和insertAdjacentText方法(方法即是某特定对象能直接调用的函数)在先前文本或Html内容的前边或后边插入新的文本或Html内容,使用这些方法需要参数,这些参数有:BeforeBegin、 AfterBegin、 BeforeEnd和AfterEnd,它们是用来标明文本或Html插入的地方。具体用法如下例: 例14 使用insertAdjacentHTML插入文本 <html> <head> <title>DHtml举例14</title> <style><!-- body {font-family:"宋体";color="blue";font-size:"9pt"} --> </style> <script language="javascript"> function Insert() { document.all.New.insertAdjacentHTML("AfterBegin","<font color=red>-新插入的内容-<font>"); //第一个参数是用来指明位置,第二个参数是要插入的Html内容。 }//function </script> </head> <body> <p><br> </p> <p id="New" onclick="Insert()">点击此行蓝色文字将插入文本</p> </body> </html> 此例的动态效果如下:
点击此行文字将插入文本
您可以试一下另外三个参数BeforeBegin、BeforeEnd和AfterEnd。insertAdjacentText方法地用法也是类似的。
Copyright © Rwchina Online network service center 2007.All Rights Reserved.