码迷,mamicode.com
首页 > 编程语言 > 详细

动态生成和移除img标签---JavaScript

时间:2017-12-28 19:50:38      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:pre   onclick   pos   control   query   elements   w3c   name   inner   

1.动态生成img标签----使用如下的方法2兼容性较好

 1 <div id="d1"></div>
 2 <script>
 3 //HTML  1
 4 function a(){
 5 document.getElementById("d1").innerHTML="<img src=‘http://baike.baidu.com/cms/rc/240x112dierzhou.jpg‘>";
 6 }
 7 a();
 8 //方法 2
 9 function b(){
10 var d1=document.getElementById("d1");
11 var img=document.createElement("img");
12 img.src="http://baike.baidu.com/cms/rc/240x112dierzhou.jpg";
13 d1.appendChild(img);
14 }
15 b();
16 //对象 3
17 function c(){
18 var cc=new Image();
19 cc.src="http://baike.baidu.com/cms/rc/240x112dierzhou.jpg";
20 document.getElementById("d1").appendChild(cc);
21 }
22 c();
23 </script>

 

 

2.动态删除img标签

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
    <head>  
        <title>jquery</title>  
  
        <meta http-equiv="pragma" content="no-cache">  
        <meta http-equiv="cache-control" content="no-cache">  
        <meta http-equiv="expires" content="0">  
        <script type="text/javascript" src="javascript/jquery-1.4.js">  
</script>  
        <script type="text/javascript" src="javascript/elements.js">  
</script>  
        <script type="text/javascript" src="javascript/jqia2.support.js">  
</script>  
        <script type="text/javascript">  
  function one(){  
    $(‘#dimg img‘).remove();  
}  
</script>  
    </head>  
    <body>  
        <div id="dimg">  
            <img src="images/image.1.jpg" id="hibiscus" alt="Hibiscus" />  
            <img src="images/image.2.jpg" id="littleBear"  
                title="A dog named Little Bear" />  
            <img src="images/image.3.jpg" id="verbena" alt="Verbena" />  
            <img src="images/image.4.jpg" id="cozmo" title="A puppy named Cozmo" />  
            <img src="images/image.5.jpg" id="tigerLily" alt="Tiger Lily" />  
            <img src="images/image.6.jpg" id="coffeePot" />  
            <img src="images/image.6.jpg" id="coffeePot" />  
        </div>  
        <div>  
          <input type="button" onclick="one()" value="button">  
        </div>  
    </body>  
</html>  

  

动态生成和移除img标签---JavaScript

标签:pre   onclick   pos   control   query   elements   w3c   name   inner   

原文地址:https://www.cnblogs.com/Nico-luo/p/8137020.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!