").appendTo("head");不会出现在生成的源代码中。如何?" />

jQuery $("").appendTo("head");不会出现在生成的源代码中。如何?

发布于 2024-11-28 03:40:19 字数 931 浏览 1 评论 0 原文

嗯,这并不是一个真正的问题,但有些东西确实让我感兴趣。

通常我们将元素附加到 head/body 中,例如

$("<style><\/style>").appendTo("head");
$("<div><\/div>").appendTo("body");

当我们查看生成的源代码时,这些元素确实存在。但是,

$("<script><\/script>").appendTo("head");

脚本元素不会出现在源代码中。

我查看了 jQuery 的源代码,除了纯 javascript 之外没有发现任何特别的东西。

append: function() {
    return this.domManip(arguments, true, function( elem ) {
        if ( this.nodeType === 1 ) {
            this.appendChild( elem ); // <--
        }
    });
},

如果我使用纯 Javascript 将脚本元素添加到 head 中,它就会显示出来。

那么 jQuery 如何做到让脚本“不可见”同时“可访问”呢?
script 标签有什么特别之处?

更新:在回答之前请注意

  • 我并不是想解决问题。
  • 我正在使用 Firebug/开发人员工具。
  • 我正在查看生成的源代码。

Well, this is not really a problem, but something really interests me.

Normally we append elements to head/body like

$("<style><\/style>").appendTo("head");
$("<div><\/div>").appendTo("body");

And when we look at the generated source code, these elements are really there. but,

$("<script><\/script>").appendTo("head");

The script element does not show up in the source code.

I looked through the source code of jQuery, and didn't find anything special but pure javascript

append: function() {
    return this.domManip(arguments, true, function( elem ) {
        if ( this.nodeType === 1 ) {
            this.appendChild( elem ); // <--
        }
    });
},

If I use pure Javascript to add a script element to head, it'll show up.

So how does jQuery do that to make the script "invisible" and meanwhile "accessible"?
What's special about the script tag?

update: note before you answer

  • I am not trying to solve a problem.
  • I AM using Firebug/Developer tools.
  • I am viewing the GENERATED source code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凯凯我们等你回来 2024-12-05 03:40:19

正如您可能首先认为的那样,这与 appendTo 并不是唯一相关的。相反,这是 jQuery 为其所有将任意 HTML 字符串插入 DOM 的函数所做的事情。

要了解为什么会发生这种情况的深入答案,请阅读 John Resig 对同一问题的回复 此处

据我所知,jQuery 实际上在将 script 标签插入 DOM 后将其删除,以避免重新执行代码。

This is not singularly related to appendTo as you might first believe. Rather, this is something that jQuery does for all of their functions that insert arbitrary HTML strings into the DOM.

To see an in-depth answer as to why this happens, read John Resig's reply to this same question here.

From what I know, jQuery actually removes the script tag after inserting it into the DOM, in order to avoid re-execution of the code.

月竹挽风 2024-12-05 03:40:19

尝试一下,

<script>
$("<scri"+"pt></sc"+"ript>").appendTo("head");
</script>

您必须从代码中“转义”它,否则浏览器会将其识别为您的结尾

Try this

<script>
$("<scri"+"pt></sc"+"ript>").appendTo("head");
</script>

you have to "escape" that from your code, otherwise browser recognizes it as end of you <script>

荒芜了季节 2024-12-05 03:40:19

大多数浏览器中的“查看源代码”选项仅向您显示页面最初加载时标记的外观。由于您在页面加载后操作 DOM,因此当您“查看源代码”时,这些更改并不容易看到。

一些 Web 开发人员工具,如 Firebug(适用于 Firefox)或 IE 开发工具栏(Internet Explorer)将通过允许您检查元素来向您显示操纵的标记。

The "View Source" option in most browsers only shows you what the markup looked like when the page initially loaded. Since you're manipulating the DOM after the page has loaded, those changes aren't readily visible when you "view source."

Some web developer tools like Firebug (for Firefox) or the IE Development Toolbar (Internet Explorer) will show you the manipulated markup by allowing you to inspect elements.

情愿 2024-12-05 03:40:19

虽然我看到已经有一个答案,但我想我应该为我发现的内容添加注释...

我遇到了类似的情况:我正在生成一个目录列表,其中列出了每个 h1、h2、h3 的链接我的网站中的元素。目录将跨整个站点的页面,并包含在单独的页面上。我组合了所有页面,然后使用 jquery 快速创建每个标题的超链接的动态有序列表,而不必手动创建它。

然后我打算将该代码复制并粘贴到它自己的页面中并使其可用 - 我在 Firefox 中遇到的问题是代码没有显示在“查看源代码”窗口中,仅显示在 firebug 检查元素 HTML 窗口中- 那么我怎样才能复制它呢?

我发现如果我使用 IE 的开发人员工具,我可以利用其开发人员部分中的 HTML 编辑选项从其元素资源管理器窗口复制并粘贴代码。

可能有更好的方法来创建每个 h1、h2、h3...h7 的链接的大型嵌套目录列表,但我对这一切都很陌生,所以走了这条路线。也许这会对某人有所帮助:)

While I see there is already an answer to this I figured I would add a note for what I found...

I was running into something similar: I was generating a table of content listing of links to each of my h1,h2,h3 elements in my website. The TOC would be across pages for the entire site and would be included on a separate page. Rather than having to manually create this I combined all pages and then used jquery to quickly create a dynamic ordered list of hyperlinks to each header.

I was going to then copy and paste that code into it's own page and make it available - The issue I was running into with firefox was that the code wasn't showing in the "view source" window, only in firebug inspect element HTML window - so how could I copy it??

I found that if I use IE's developer tools I could utilize the HTML Edit option in their developer section to copy and paste the code from their element explorer window.

There is probably a better way to create a large nested table of content list of links to each h1,h2,h3...h7 but I'm new to all this and went this route. Maybe this will help someone :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文