2013-05-16 175 views
0

看一看这段代码:如何获取iframe元素的实际代码(不是内部内容)?

http://jsfiddle.net/NTYcA/1/

var foo = '<iframe width="560" height="315" src="http://www.youtube.com/embed/A175-KHvfy4" frameborder="0" allowfullscreen></iframe>'; 

foo = $(foo); // turned into jquery object 

alert(foo.html()); // alerts nothing 
alert(foo[0]);  // alerts [Object HTMLIFRAMELEMENT] but how can I get the actual iframe code as a string? 

所以,我怎样才能从jQuery对象的iframe代码?

我真的想这样的:

<iframe width="560" height="315" src="http://www.youtube.com/embed/A175-KHvfy4" frameborder="0" allowfullscreen></iframe> 

,而无需访问原来的变种。

回答

3

有jQuery中没有outerHTML,所以你必须做这样的事情:

var html = $('<div />').append(foo.clone()).html()