如何在Jquery中将HTML字符串转换为HTMLDivElement对象? [英] How to convert HTML string to HTMLDivElement Object in Jquery?
问题描述
HTML代码以字符串格式从api函数返回.
var div = infoWindow.getContent(); div变量包含以下代码.
HTML code is returning from api function as a string format.
var div = infoWindow.getContent(); the div variable contains the following code.
<div class="mt">
<div class="fl">
<a id="detailsLink" class="jsDetailsLink" href="/profile/369/john">More profile information</a>
</div>
</div>
.
我想转换为jQuery
中的 [object HTMLDivElement] .如果将其转换为对象HTMLDivElement
,我可以通过 JS代码轻松更改a href值.
如何转换它?
.
I want to convert to [object HTMLDivElement] in jQuery
. If it is converted into object HTMLDivElement
, I can easily change the a href value through JS code.
How to convert that?
推荐答案
请考虑以下字符串:
var string = '<div class="mt"><div class="fl"><a id="detailsLink" class="jsDetailsLink" href="/profile/369/john">More profile information</a></div></div>';
您可以将其传递给$
函数,而jQuery的力量将在您手中:
You can pass it to $
function, and jQuery power will be in your hands:
var $elem = $(string);
现在您可以在其中找到链接:
Now you can get the link within it:
$elem.find('a').attr('href', 'http://www.google.com/');
并将其附加到某些元素上:
And append it to some element:
$elem.appendTo('body');
-
[!]
您可以在此 FIDDLE 上进行检查. [!]
You can check it at this FIDDLE.
这篇关于如何在Jquery中将HTML字符串转换为HTMLDivElement对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!