简体   繁体   English

如何将[Object HTML]解析为HTML字符串

[英]How to parse [Object HTML] to HTML String

I have a javascript variable 我有一个JavaScript变量

inputElem[i] inputElem [I]

when I combine it with other string like: 当我将其与其他字符串结合在一起时:

var result = inputElem[i]  + '<div>xv</div>';

expected result : 预期结果

<input type="radio" value="www.abc.com"> 
<div>xv</div>

actually : 实际上

[object HTMLInputElement] 
<div>xv</div>

The problem is inputElem[i] is a dom element, so the default to string implementation is to blame here. 问题是inputElem[i]是一个dom元素,因此在此归咎于字符串实现的默认设置。

Looks like you want the wrapping html markup for the element so use outerHTML 看起来您想要元素的包装html标记,所以请使用externalHTML

var result = inputElem[i].outerHTML  + '<div>xv</div>';

when, you try to serialize (convert to string) any object, its datatype is what you get as a result 当您尝试序列化(转换为字符串)任何对象时,其数据类型就是您得到的结果

try this: 尝试这个:

var result = $('<div>').append(inputElem[i]).clone()).html()+ '<div>xv</div>';

or 要么

 var result = $(inputElem[i]).prop('outerHTML')

or with plain javascript, you can simply do outerHtml ie 或使用简单的javascript,您只需执行outerHtml

var result = inputElem[i].outerHTML  + '<div>xv</div>';

In your case, you should concatentate the HTML code for input object, not the input object itself. 对于您的情况,您应该为输入对象而不是输入对象本身添加HTML代码。

Use JQuery html() function: 使用JQuery html()函数:

$(inputElem[i]).html() + '<div>xv</div>';

Try this: 尝试这个:

var result = $(inputElem[i]).html() + '<div>xv</div>';
$('#parentID').html(result);

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

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