2011-01-14 26 views
1

我要拿起值div标签中像这是一些什么样的这个jQuery是不是从div标签捡了正确的价值观

<html> 
<head> </head> 
<body> 
<div id="page"> 
       <html> 
       <head></head> 
       <body> Hellow World </body> 
       </html> 
</div> 
</body> 
</html> 

我想选择一个div标签里面的内容。

var msg = $("#page").html(); 
alert(msg); 

此代码不起作用。我希望整个第二页和HTML标签一起被复制。我怎么做 ?

我所要的输出是包括HTML标记

+1

这里的问题是你打破了如何工作的DOM,然后期待JavaScript工作。我不认为这个人能够可靠地工作。 – 2011-01-14 16:29:43

+0

它应该工作。你在`$(document).ready`函数中调用了这个函数吗?当您调用警报时,页面可能不完整。你有没有从警报中得到任何东西? – Cfreak 2011-01-14 16:30:37

+0

重读您的问题后,我不确定是否正确理解它。你正在寻找#page div内的html代码的字符串表示形式吗?如果是这样,你可能会遇到问题,因为#page div中的一些元素不能在其内部。大多数浏览器都会将它们删除。 – 2011-01-14 16:36:51

回答

1

使用文本()而不是HTML()整个事情。

var msg = $("#page").text(); 
alert(msg); 
// Hellow World 
1

不能使用<html><body>标签这样的 - 只能有每个之一,在非常明确的立场。你想使用iframe吗?

当浏览器解析该页面时,它将忽略内部<html><body>标签。

如果你想在页面中有页面,你需要使用<iframe>

0

不能使用HTML标记中的HTML

最好使用iframe中的HTML编写HTML里面的html

您可以使用Iframe这样

<html> 
<head> 
</head> 
<body> 
    <font face="verdana,arial" size="3"> 
    <div align="center"> 
    <p> 
     <a href="http://www.altavista.com" target="iframe1">AltaVista.com</a> | 
     <a href="http://www.aol.com" target="iframe1">AOL.com</a> | 
     <a href="http://www.lycos.com" target="iframe1">Lycos.com</a> | 
     <a href="http://www.yahoo.com" target="iframe1">Yahoo.com</a> 

    <p> 
    <iframe name="iframe1" width="600" height="400" src="http://www.stackoverflow.com" frameborder="yes" scrolling="yes"> 
    </iframe> 
    <p> 

</font> 
</div> 
<!--name="iframe1" 
- Set all links to target the iframe1 name when you want the contents of the iframe to change from links on the originating page. 

width="600" - Width of the iframe 

height="400" - Height of the iframe * Width and height can be expressed in pixels or percent 

src="http://www.yahoo.com" - Initial page that will fill in the iframe 

frameborder="yes" - Border around the iframe is displayed by default 

scrolling="yes" - Allow scrollbars around the iframe for navigation Vaild values are yes, no and auto 

align="" - Valid entries are left and right left is set by default 
--> 

</body> 
</html> 
0

或者,如果你想避免的(它有它自己的问题),仍然使用正确的HTML,把内容作为隐藏的值<textarea>。然后你可以使用.val()