2012-07-06 84 views
0

可以帮助我请jQuery对象问题。jQuery对象强制转换为字符串

问题是下面,我有这样的代码:

url = '<a href="http://url.com">Name</a>'; 
otherValue = "Other Value"; 

x = jQuery(url).text(otherValue); 
console.log(x);   
console.log(typeof(x)); 

这回:

[<a href=​"http:​/​/url.com">​Other Value​</a>​] 
object 

我如何作出投这个对象,最后得到一个字符串?

感谢的

回答

2

尝试console.log(x[0].outerHTML);

解释:x[0]给你HTMLAnchorElement对象,HTMLAnchorElement.outerHTML给你的HTML字符串。

+0

Perfect!!!!!! Thanks – Marcelo 2012-07-06 03:44:12

+0

Now I have this problem: may be than outerHTML replace me ">" "<" characters ?? <span class="ac_match">Name</span> String表示 – Marcelo 2012-07-06 03:50:25

0

看看这个链接。

url = '<a href="http://url.com">Name</a>'; 
otherValue = "Other Value"; 

x = jQuery(url).text(otherValue); 
alert(x[0].outerHTML); 

http://jsfiddle.net/SRjfq/