简体   繁体   English

使用 CSS 样式表更改在 Javascript 中添加的图像的位置

[英]Changing the Location of an Image added in Javascript with a CSS stylesheet

I'm still pretty new to HTML and the languages commonly associated with it.我对 HTML 和通常与之相关的语言仍然很陌生。 Right now, I'm trying to adjust the location of an image I added in through a function defined in Javascript like I usually would in CSS with something like margin-left: 100px .现在,我正在尝试通过 Javascript 中定义的函数来调整我添加的图像的位置,就像我通常在 CSS 中使用margin-left: 100px

EDIT: Added code and clarification编辑:添加代码和说明

case 1:
        demo.innerHTML = "You win cookies!";
        var img = new Image();
        img.src = 'cookies.jpg';
        img.onclick = function() {
        window.location.href = 'http://link.com/';}

document.body.appendChild(img); document.body.appendChild(img);

You can set properties to position elements via Javascript, eg:您可以通过 Javascript 设置属性来定位元素,例如:

var elem = document.getElementById("elementid"); elem.style.left=200; elem.style.top=200;

you may need to set:您可能需要设置:

elem.style.position='absolute';

 var img = new Image(); img.src = 'http://data.whicdn.com/images/27433830/large.jpg'; img.id = "myImage"; // <-- give it an ID img.onclick = function() { window.location.href = 'http://link.com/'; } document.body.appendChild(img); document.getElementById("button").onclick = function() { var image = document.getElementById("myImage"); image.src = "http://weknowyourdreamz.com/images/house/house-07.jpg"; }
 <button id="button">switch</button>

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

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