脚本宝典收集整理的这篇文章主要介绍了js实例Document 对象(4),脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。
源代码
<!DOCTYPE htML> <html> <body> The full URL of this document is: <script> document.wrITe(document.URL); </script> </body> </html>
运行结果
打开输出流,向流中输入文本
源代码
<!DOCTYPE html> <html> <head> <script> function createDoc() { VAR doc=document.open("text/html","replace"); var txt="<!DOCTYPE html><html><body>Learning about the HTML DOM is fun!</body></html>"; doc.write(txt); doc.close(); } </script> </head> <body> <input type="button" value="New document" onclick="createDoc()"> </body> </html>
运行结果
write() 和 writeln()的不同
源代码
<!DOCTYPE html> <html> <body> <p>Note that write() does NOT add a new line after each statement:</p> <PRe> <script> document.write("Hello World!"); document.write("Have a nice day!"); </script> </pre> <p>Note that writeln() add a new line after each statement:</p> <pre> <script> document.writeln("Hello World!"); document.writeln("Have a nice day!"); </script> </pre> </body> </html>
运行结果
用指定的ID弹出一个元素的innerHTML
源代码
<!DOCTYPE html> <html> <head> <script> function getValue() { var x=document.getElementById("myHeader"); alert(x.innerHTML); } </script> </head> <body> <h1 id="myHeader" onclick="getValue()">Click me!</h1> </body> </html>
运行结果
用指定的Name弹出元素的数量
源代码
<!DOCTYPE html> <html> <head> <script> function getElements() { var x=document.getElementsByName("x"); alert(x.length); } </script> </head> <body> Cats: <input name="x" type="radio" value="Cats"> Dogs: <input name="x" type="radio" value="Dogs"> <input type="button" onclick="getElements()" value="How many elements named 'x'?"> </body> </html>
运行结果
用指定的tagname弹出元素的数量
源代码
<!DOCTYPE html> <html> <head> <script> function getElements() { var x=document.getelementsbytagname("input"); alert(x.length); } </script> </head> <body> <input type="text" size="20"><br> <input type="text" size="20"><br> <input type="text" size="20"><br><br> <input type="button" onclick="getElements()" value="How many input elements?"> </body> </html>
运行结果
源代码
<!DOCTYPE html> <html> <body> The full URL of this document is: <script> document.write(document.URL); </script> </body> </html>
运行结果
打开输出流,向流中输入文本
源代码
<!DOCTYPE html> <html> <head> <script> function createDoc() { var doc=document.open("text/html","replace"); var txt="<!DOCTYPE html><html><body>Learning about the HTML DOM is fun!</body></html>"; doc.write(txt); doc.close(); } </script> </head> <body> <input type="button" value="New document" onclick="createDoc()"> </body> </html>
运行结果
write() 和 writeln()的不同
源代码
<!DOCTYPE html> <html> <body> <p>Note that write() does NOT add a new line after each statement:</p> <pre> <script> document.write("Hello World!"); document.write("Have a nice day!"); </script> </pre> <p>Note that writeln() add a new line after each statement:</p> <pre> <script> document.writeln("Hello World!"); document.writeln("Have a nice day!"); </script> </pre> </body> </html>
运行结果
用指定的ID弹出一个元素的innerHTML
源代码
<!DOCTYPE html> <html> <head> <script> function getValue() { var x=document.getElementById("myHeader"); alert(x.innerHTML); } </script> </head> <body> <h1 id="myHeader" onclick="getValue()">Click me!</h1> </body> </html>
运行结果
用指定的Name弹出元素的数量
源代码
<!DOCTYPE html> <html> <head> <script> function getElements() { var x=document.getElementsByName("x"); alert(x.length); } </script> </head> <body> Cats: <input name="x" type="radio" value="Cats"> Dogs: <input name="x" type="radio" value="Dogs"> <input type="button" onclick="getElements()" value="How many elements named 'x'?"> </body> </html>
运行结果
用指定的tagname弹出元素的数量
源代码
<!DOCTYPE html> <html> <head> <script> function getElements() { var x=document.getElementsByTagName("input"); alert(x.length); } </script> </head> <body> <input type="text" size="20"><br> <input type="text" size="20"><br> <input type="text" size="20"><br><br> <input type="button" onclick="getElements()" value="How many input elements?"> </body> </html>
运行结果
觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的js实例Document 对象(4)全部内容,希望文章能够帮你解决js实例Document 对象(4)所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。