admin管理员组文章数量:1335427
I'm using javascript to reach img elements nested in some <div>
.After that I want to add those images to a string variable.
I wrote that:
var SomeVariable="";
$("myDivId img").each(function(){
console.log(this);
SomeVariable += this;
});
console.log(SomeVariable);
When console.log
is used in .each
function, it shows something like:
<img (some elements)>
, which is exactly what I want.
When I use console.log
at the end, to write whole value, it says:
[object HTMLImageElement][object HTMLImageElement]
I tried to use some conversion, but I don't really know how to get to it.
I'm using javascript to reach img elements nested in some <div>
.After that I want to add those images to a string variable.
I wrote that:
var SomeVariable="";
$("myDivId img").each(function(){
console.log(this);
SomeVariable += this;
});
console.log(SomeVariable);
When console.log
is used in .each
function, it shows something like:
<img (some elements)>
, which is exactly what I want.
When I use console.log
at the end, to write whole value, it says:
[object HTMLImageElement][object HTMLImageElement]
I tried to use some conversion, but I don't really know how to get to it.
Share Improve this question edited Oct 16, 2012 at 11:40 dandan78 13.9k13 gold badges67 silver badges81 bronze badges asked Oct 16, 2012 at 11:34 Kamil TKamil T 2,2161 gold badge19 silver badges27 bronze badges 2- 1 Do you want the string representation of their HTML? – alex Commented Oct 16, 2012 at 11:37
-
DOM elements are not HTML. The console just represents them with HTML syntax for visual purposes. The correct
toString()
value of the element is[object HTMLImageElement]
. – I Hate Lazy Commented Oct 16, 2012 at 11:43
1 Answer
Reset to default 7Assuming you want the string representation of their HTML...
var html = $("myDivId img").clone().appendTo("<div />").html();
If you're only supporting browsers which have the outerHTML
property.
var html = $("myDivId img")
.map(function() { return this.outerHTML; }).get().join("");
The reason you get "[object HTMLImageElement]"
is because an Object
's toString()
will give you "[object x]"
, where x
is the [[Class]]
(an internal property) of the object.
本文标签: javascriptCan39t convert HTMLImageElement to stringStack Overflow
版权声明:本文标题:javascript - Can't convert HTMLImageElement to string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742243544a2438986.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论