【问题标题】:jQuery is not picking up correct values from a Div TagjQuery 没有从 Div 标签中获取正确的值
【发布时间】:2011-01-14 16:26:29
【问题描述】:

我想在一个 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 标签在内的全部内容

【问题讨论】:

  • 这里的问题是你破坏了 DOM 的工作方式,然后期望 javascript 工作。我不认为这个会可靠地工作。
  • 它应该可以工作。你在$(document).ready 函数中有这个调用吗?当您调用警报时,页面可能不完整。您是否从警报中得到任何信息?
  • 重读你的问题后,我不确定我是否理解正确。您是否在#page div 中寻找 html 代码的字符串表示形式?如果是这样,您可能会遇到问题,因为#page div 中的某些元素不能在它们自身内部。大多数浏览器都会删除它们。
  • @Cfreak = 是的,我在 $(document).ready 函数中使用了它
  • 我希望 Div 标签将其中的所有内容都视为字符串(HTML 标签等)

标签: javascript jquery html tags selection


【解决方案1】:

使用 text() 代替 html()。

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

【讨论】:

  • 我希望显示 Div 标签中的整个 HTML 标签:)
【解决方案2】:

您不能像那样使用&lt;html&gt;&lt;body&gt; 标签 - 每个标签只能有一个,位置非常明确。你想使用 iframe 吗?

当浏览器解析该页面时,它会忽略内部的&lt;html&gt;&lt;body&gt; 标签。

如果您想在一个页面中有一个页面,您需要使用&lt;iframe&gt;

【讨论】:

  • 除了 iframe 就没有别的办法了吗?
  • 嗯,一个很好的老&lt;frameset&gt; 可以达到类似的结果,但我会避免它。但是有多个这样的标签是无效的HTML。
  • 我是用PHP函数写内除的内容? (HTML 标签来自 PHP 函数 file_get_contents() )我如何在 IFRAME 中显示它
  • 我试图不使用 iframe,因为我的 JavaScript 在 iframe 中不起作用
  • 如果您需要在另一个页面中有一个完整的页面,您需要一个 iframe。如果您对该 iframe 中的 JavaScript 有问题,那么这是一个不同的问题。但从这个基础开始。
【解决方案3】:

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

【讨论】:

  • 我是用PHP函数写内除的内容? (HTML 标记来自 PHP 函数 file_get_contents())我如何在 IFRAME 中显示它?
  • 我的建议是在插入内容的函数中添加 php 代码以删除 body 之前和 /body 之后的所有内容。
  • 您将无法按照说明在 iframe 中显示它。尝试使用文本区域。 &lt;textarea id="page_content" name="page_content" value="&lt;%= file_get_contents() %&gt;"&gt;。要在 iframe 中显示它,您需要另一个仅呈现 file_get_contents() html 的 url,然后将 iframe 的 src 属性设置为该 url。
  • 文本区域不会将文本显示为 HTML 。这就是问题:)
【解决方案4】:

你不能在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>

【讨论】:

  • 我是用PHP函数写内除的内容? (HTML 标记来自 PHP 函数 file_get_contents())我如何在 IFRAME 中显示它?
  • 我试图不使用 Iframe,因为 Java Scripts 在 Iframe 中不起作用
  • 您可以在 iframe 中使用 javascript 查看此链接 devarticles.com/c/a/JavaScript/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多