[英]Changing the Location of an Image added in Javascript with a CSS stylesheet
我对 HTML 和通常与之相关的语言仍然很陌生。 现在,我正在尝试通过 Javascript 中定义的函数来调整我添加的图像的位置,就像我通常在 CSS 中使用margin-left: 100px
。
编辑:添加代码和说明
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);
您可以通过 Javascript 设置属性来定位元素,例如:
var elem = document.getElementById("elementid"); elem.style.left=200; elem.style.top=200;
您可能需要设置:
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.