简体   繁体   English

Javascript /在页面上获取图片元素,但在div中获取图片元素(getElementByID)

[英]Javascript / Get an image element on page and but it in div (getElementByID)

For a banner, I need to put an image who are displayed on the same page. 对于横幅,我需要放置在同一页面上显示的图像。 I'm really bad as developpement - So, I use this way - But, indeed, it doesn't work... 我的发展真的很糟糕-所以,我用这种方式-但是,实际上,它不起作用...

<!doctype html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta name="ad.size" content="width=300,height=600">
  <title>Stack</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
  <a id="clickThroughBtn" href="javascript:window.open(window.clickTag)">
  <div id="border">
    <div id="container">
      <div class="border2">
        <div id="logo_bg"></div>
        <div id="red_c" class="logo_element"></div>
        <div id="rays" class="logo_element"></div>
        <div class="logo"><img src="logo_fr.svg" alt="Cofidis" /></div>
        <div class="title">
          Test
          <div id="adaptive">
            <script>
              function getImgs(){
                var Adaptive = document.getElementsByID('thumb0').img;
                Adaptive[0].style.width="200px"; // Taille de l'image
                Adaptive[0].style.width="200px"; // Hauteur
              }
            </script>
          </div>
        </div>
      </div>
    </div>
    </a>
    <img id="thumb0" src="http://gmz2.zarbi.be/avatars/m/1/1116.jpg?1404045273.jpg" />
</html>

The most important part - 最重要的部分-

<!-- Adaptive -->
<div id="adaptive">
  <script>
    function getImgs(){
      var Adaptive = document.getElementsByID('thumb0').img;
      Adaptive[0].style.width="200px"; // Taille de l'image
      Adaptive[0].style.width="200px"; // Hauteur
    }
  </script>
</div>
<!-- Adaptive -->

Thank you everyone ! 谢谢大家 ! Ludovic 鲁多维奇

You need to change this: 您需要更改此:

var Adaptive = document.getElementsByID('thumb0').img;
Adaptive[0].style.width="200px";                       // Taille de l'image
Adaptive[0].style.width="200px";                       // Hauteur

To this: 对此:

var Adaptive = document.getElementById('thumb0');
Adaptive.style.width="200px";                         // set image width
Adaptive.style.height="200px";                        // set image height

as there is no such method getElementsByID in the document object. 因为文档对象中没有此类方法getElementsByID

Also, getElementById returns a single element. 另外,getElementById返回单个元素。 If you need to get multiple objects, then getElementsByName , getElementsByClassName and getElementsByTagName will work for you. 如果需要获取多个对象,则getElementsByNamegetElementsByClassNamegetElementsByTagName将为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM