js为加载图片的实现过程

         通过这个操作,自己对js有了更深一层的理解.js是什么呢?

         js是操作浏览器的语言.有什么用呢?

         可以在客户的游览器动态加载一些内容上去.它是怎么样做的?

        先要明白一些基本的原理,游览器显示html文本,是将它的<>标签都在内存中建成对象用来显示.加上游览器本身的对象,我们就可以用js用操作这些对象,从而达到添加删除html标签对象,给html对象的属性赋值,加内容等.

        以下是详细的操作过程.

        一.问题提出,想将下面代码改为用js在运行时加载.

            

  1. <html>
  2. <head>
  3. <title>
  4. test marquee!
  5. </title>
  6. </head>
  7. <body>
  8. <marquee  id="zhao" direction="up" scrollamount=6 scrolldelay=50 scrollAmount=10  onmouseout="this.start()" onmouseover="this.stop()" width="177px" style="height: 540px">
  9. <img id="ImgUserControl1_img1" border="0" src="DefaultImg/企业首页图片1.jpg" style="height:150px;width:177px;border-width:0px;WIDTH: 177px; HEIGHT: 150px" /><BR />
  10. <img id="ImgUserControl1_img2" border="0" src="DefaultImg/企业首页图片2.jpg" style="height:150px;width:177px;border-width:0px;WIDTH: 177px; HEIGHT: 150px" /><BR />
  11. <img id="ImgUserControl1_img3" border="0" src="DefaultImg/企业首页图片3.jpg" style="height:150px;width:177px;border-width:0px;WIDTH: 177px; HEIGHT: 150px" /><BR />
  12. <img id="ImgUserControl1_img4" border="0" src="DefaultImg/企业首页图片4.jpg" style="height:150px;width:177px;border-width:0px;WIDTH: 177px; HEIGHT: 150px" /><BR />
  13. <img id="ImgUserControl1_img5" border="0" src="DefaultImg/企业首页图片5.jpg" style="height:150px;width:177px;border-width:0px;WIDTH: 177px; HEIGHT: 150px" /><BR />
  14. </marquee>
  15. </body>
  16. </html>

        二.查看 DHTML对象,看marquee标签对象有什么方法可以在用js 得到后用来加入文本.有什么事件可以起到运行时加载的.

        A:添加html内容(insertAdjacentHTML和insertAdjacentText)

            运行时触发的事件是body 的onload

        http://www.phpx.com/man/dhtmlcn/objects/BODY.html

        http://www.w3school.com.cn/js/js_reference.asp

       三.查一下这方法怎么样用.因为上面网站查到的只有方法而没有参数.

      

原型:insertAdajcentHTML(swhere,stext)

insertAdjacentHTML方法:在指定的地方插入html标签语句
参数:
swhere: 指定插入html标签语句的地方,有四种值可用:
1.beforeBegin: 插入到标签开始前

2.afterBegin:插入到标签开始标记之后

3.beforeEnd:插入到标签结束标记前

4.afterEnd:插入到标签结束标记后

stext:要插入的内容

 

 insertAdjacentText方法与insertAdjacentHTML方法类似,只不过只能插入纯文本,参数相同

  1. <html>
  2.    <head>
  3.     <script language="javascript">
  4.        function myfun()
  5.          {
  6.              var obj = document.getElementById("btn1");
  7.               obj.insertAdjacentHTML("afterEnd","<br><input name=/"txt1/">");
  8.          }
  9.     </script>
  10.    </head>
  11.    <body>
  12.      <input name="txt">
  13.      <input id="btn1" name="btn1" type="button" value="更多..." onclick="myfun()">
  14.    </body>
  15. </html>

    http://hi.baidu.com/inpunis/blog/item/9016fe2de0b07531349bf794.html

                四..写出了代码

             

  1. <html>
  2. <head>
  3. <title>
  4. test marquee!
  5. </title>
  6. <SCRIPT language=javascript>
  7.  function myfun()
  8. {
  9. var zhaodocument.getElementById("zhao"); 
  10. for(i = 1;i<=5;i++)
  11. {
  12. zhao.insertAdjacentHTML("beforeEnd","<img id='ImgUserControl1_img" + i + "' border='0' src='DefaultImg/企业首页图片" + i + ".jpg' style='height:150px;width:177px;border-width:0px;WIDTH: 177px; HEIGHT: 150px' /><BR />");
  13. }
  14. }
  15. </SCRIPT>
  16. </head>
  17. <body onload="myfun()">
  18. <marquee  id="zhao" direction="up" scrollamount=6 scrolldelay=50 scrollAmount=10  onmouseout="this.start()" onmouseover="this.stop()" width="177px" style="height: 540px">
  19. </marquee>
  20. <input id="btn1" name="btn1" type="button" value="更多..." onclick="myfun()"> 
  21. </body>
  22. </html>

         js还可以用HTML元素的属性去设置

innerHTML

设置或获取位于对象起始和结束标签内的 HTML。

 

innerText

设置或获取位于对象起始和结束标签内的文本。

 

outerHTML设置或获取对象及其内容的 HTML 形式。

outerText设置或获取对象的文本。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值