jQuery没有从Div标签中获取正确的值

时间:2011-01-14 16:26:30

标签: javascript jquery html tags selection

我想在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标记的整体内容

4 个答案:

答案 0 :(得分:1)

使用text()代替html()。

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

答案 1 :(得分:1)

您不能使用<html><body>这样的标签 - 每个标签中只能有一个标识位置非常明确。你想使用iframe吗?

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

如果您想在页面中设置页面,则需要使用<iframe>

答案 2 :(得分:0)

你不能在html中使用html标签

最好在html中使用iframe在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>

答案 3 :(得分:0)

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

相关问题