[英]How to append an image at the beginning of an HTML tag?
I am using JavaScript. 我正在使用JavaScript。
I have the link below in my HTML page: 我的HTML页面中有以下链接:
Now I want to append the green dollar sign before the start of the text and not below it. 现在,我想在文本开头之前而不是下方添加绿色的美元符号。 I currently have the code below:
我目前有以下代码:
var imgElement = document.createElement("img");
imgElement.setAttribute("src",server+"images/dollar-green.png");
anchor.appendChild(imgElement);
您需要使用insertBefore
anchor.insertBefore(imgElement);
Maybe You should try insertBefore()
. 也许您应该尝试
insertBefore()
。 Change last line of Your code to this: 将您的代码的最后一行更改为:
anchor.insertBefore(imgElement, anchor.childNodes[0]);
and then add CSS code on <img>
tag: 然后在
<img>
标签上添加CSS代码:
img {
float: left;
}
or 要么
img {
display: inline-block;
}
or via JavaScript: 或通过JavaScript:
imgElement.setAttribute('style','float:left;');
尝试这个:
anchor.insertBefore(imgElement, anchor.childNodes[0])
Easy way of doing this is to use the float style attribute on your image 简单的方法是在图片上使用float样式属性
var anchor = document.getElementById('theAnchor'); var imgElement = document.createElement('img'); imgElement.setAttribute('src','http://www.easyicon.net/api/resize_png_new.php?id=139&size=32'); imgElement.setAttribute('style','float:left;'); anchor.appendChild(imgElement);
<a id="theAnchor" style="font-weight:bold;font-family:Arial;font-size:26px;color:#ff0000;"> Sample Text </a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.