Javascript 是否具有给定文档的原始源代码的内部知识? [英] Does Javascript have internal knowledge of the raw source code of a given document?

查看:23
本文介绍了Javascript 是否具有给定文档的原始源代码的内部知识?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html>
 <head>
   <script>
     remove_line(11);
     // This should remove the line 11 from source code, 
     // eliminating the unordered list element.
   </script>
 </head>
 <body>
   <p>Some text</p>
   <ul><li>Some list item</li></ul>
   <a>Some link</a>
 </body>
</html>

完全意识到此代码会伤害您的眼睛.但是,由于非常非典型的技术原因(主要是性能),这将是解决我的问题的最有效方法.这很可能不可行,但确实可以为我省去很多编码和性能问题.

I am totally aware that this code hurts your eyes. However, for very atypical technical reasons (performance mostly), this would be the most effective way to solve my problem. It is most likely not doable but it would really save me a lot of coding and performance issues.

如果不可行(如预期的那样),唯一识别/访问 DOM 的每个元素(包括文本)的最一致/最有效的符号/技术是什么?与元素混合/内联).

If it is not doable (as expected), what is the most consistent/effective notation/technique for uniquely identifying/accessing every elements of the DOM (including text that is mixed/inline with elements).

我正在考虑以下符号:tag_name[index] 其中 index 将按源代码中的出现顺序表示第 X 个元素.但是,我不确定它是否完全有效,也不确定如何在 Javascript 中实现它.我也考虑过 XPath,但我不确定 Javascript 是否能很好地支持它(除了一些模拟"它的框架).

I was thinking of the following notation: tag_name[index] where index would represent the Xth element by order of appearance in the source code. However, I'm not sure if it's exactly efficient and I'm not sure how to implement it in Javascript. I've also thought of XPath but I'm not sure if it's well supported by Javascript (apart from some frameworks "simulating" it).

更新:我原来的帖子不是很清楚,所以我会澄清一些要点:

UPDATE: My original post wasn't very clear, so I'll clarify some points:

  • 我正在开发的库的最终目标是最小化页面渲染",因此在服务器端执行并将其发送回用户不是一种选择.一旦我对我正在尝试做的事情有了一个功能性的实现,我就会发布更新.否则在这里解释太长了.

  • The ultimate goal of the library I'm working on is to "minimize page rendering", so doing it server side and sending it back to the user isn't an option. I'll post an update once I have a functional implementation of what I'm trying to do. Otherwise it'd be too long to explain here.

为了阐明我真正需要做的事情,让我举以下(虚构的)示例:

To clarify what I really need to do, let me give the following (fictional) example:

  • Script X 是一个服务器端脚本,它从 document.html 中随机选择一个 DOM 节点.
  • 脚本 X 需要告诉脚本 Y(位于 document.html 中的 Javascript 脚本)删除它选择的 DOM 节点.
  • 脚本 X 如何唯一标识它所选择的 DOM 节点,以便将其传达给脚本 Y?

我对如何唯一标识 DOM 节点很感兴趣,以便脚本 Y 可以识别并操作它.最好,它也适用于文本节点.

I'm really interested in how to uniquely identify the DOM node so that script Y can identify it and manipulate it. Preferably, it should work with text nodes as well.

推荐答案

没有 javascript 无法访问源代码.为什么不在服务器上执行此操作?

No javascript has no such access to the source code. Why don't you do this on the server instead?

这篇关于Javascript 是否具有给定文档的原始源代码的内部知识?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆