如何获取javascript中注释元素的句柄? [英] How do I get the handle to a comment element in javascript?

查看:56
本文介绍了如何获取javascript中注释元素的句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个侧面项目,需要我编写一些javascript代码。不幸的是,我对javascript世界很新,我正在学习。



所以我要做的部分工作要求我在页面上找到某些评论,并且我使用以下行来获取所有评论:

  var arr = document.getElementsByTagName(!); 

这在IE8中完美运行,并返回页面上所有注释的数组。但是,这会在Google Chrome中返回一个空数组,当我打开开发者控制台时,我可以看到未被捕获的 TypeError (可能是由于在空数组上执行了操作)。 / p>

为什么这两种浏览器都无法正常工作?这是我不应该使用的某种不受支持的操作,还是我只是在滥用它?



任何帮助,或指向某个方向的指针都非常感谢!谢谢!



澄清:
我这样做是为了操纵sharepoint页面。这个脚本的实际意图是隐藏输入元素所在的表行。所以要做到这一点我需要先找到该元素,但我不能这样做,因为它的属性基本上是乱码垃圾:

 <输入名称=ctl00 $ m $ g_edc51673_bad2_46d5_9a65_e71137e56558 $ ctl00 $ ctl04 $ ctl00 $ ctl00 $ ctl 00 $ ctl04 $ ctl00 $ ctl00 $ TextField
type =textvalue =Titlemaxlength =255
id =ctl00_m_g_edc51673_bad2_46d5_9a65_e71137e56558_ctl00_ctl04_ctl00_ctl00_ctl00_ ctl04_ctl00_ctl00_TextField
title =Titleclass =ms-long >

(我在某些地方添加换行符以便于阅读,但这一切都在一条线上在源头(不是那会产生影响))



所以无论如何,我无法真正使用它找到我正在寻找的控件。你可能会说,它有一些结构,它与垃圾结尾处的输入类型一致,而title属性和所有这些结构。但事实并非如此。你真的,微软,这是为了一些控制,但不是为其他人。



但是!它们与在这个带有元素名称的乱码垃圾之前提供评论绝对一致,如果我能抓住那个评论,我可以foo.parenNode到我想要的。

解决方案

您可以从节点开始遍历DOM, document 并迭代每个节点的 childNodes 数组。当你找到一个节点时,你可以测试 nodeType == 8



以下是

childNodes



nodeType



document.createTextNode



一个小例子

 < html> 
< head>
< / head>
< body>
< ul>
< li> junk
< li> text
<! - comment 1 - >
< li> hi
< li> hello
< li> world
< / ul>
< br />
<! - 评论2 - >
< script>
window.onload = findcommentsstart
var msgs = [];
函数findcommentsstart()
{
findcomments(document);
var txt = document.createTextNode(msgs.join(\ n));
document.getElementsByTagName(body)[0] .appendChild(txt);
}

函数findcomments(d)
{
if(!d)
return;
if(d.nodeType == 8)
msgs.push(找到一个评论节点+ d.data);
if(!d.childNodes)
return;
for(var i = 0; i< d.childNodes.length; i ++)
findcomments(d.childNodes [i]);
}
< / script>
< / body>
< / html>


I'm working on a side project at work which requires me to write some javascript code. Unfortunately I'm very new to the javascript world and am learning as I go.

So part of what I have to do requires me to find certain comments on the page, and I am using the following line to get all the comments first:

var arr = document.getElementsByTagName("!");

This works perfectly in IE8 and returns an array of all the comments on the page. However this returns an empty array in Google Chrome and I can see an uncaught TypeError (Presumably due to operations being performed on the empty array) when I open up the Developer Console.

Any ideas why this is not working on both browsers? Is this some sort of unsupported operation that I shouldn't be using, or am I just misusing it?

Any help, or pointers in some direction are greatly appreciated! Thank you!

Clarification: I am doing this in order to manipulate a sharepoint page. That actual intent of this script is to hide a table row that an input element is in. So to do that I need to first find the element, but I can't do that since the attributes for it are basically garbled junk:

<input name="ctl00$m$g_edc51673_bad2_46d5_9a65_e71137e56558$ctl00$ctl04$ctl00$ctl00$ctl‌​00$ctl04$ctl00$ctl00$TextField"
type="text" value="Title" maxlength="255"
id="ctl00_m_g_edc51673_bad2_46d5_9a65_e71137e56558_ctl00_ctl04_ctl00_ctl00_ctl00_‌​ctl04_ctl00_ctl00_TextField"
title="Title" class="ms-long">

(I put in newlines in some places to make it easier to read, but that's all on one line in the source (not that that makes a difference))

So anyway, I can't really use that find the control I'm looking for. And you may say that that has some structure what with the type of the input consistently at the end of the garbage, and the title attribute and all that. But it isn't. Yours truly, Microsoft, does this for some controls, but not for others.

BUT! They are absolutely consistent with providing comments right before this garbled junk that have the name of the element in them, and if I can grab that comment I can foo.parenNode over to what I want.

解决方案

You can traverse the DOM starting at the node, document and iterating each node's childNodes array. When you find a node, you can test for the nodeType==8

Here are links to

childNodes

nodeType

document.createTextNode

And a small example

<html>
<head>
</head>
<body>
<ul>
<li>junk
<li>text
<!-- comment 1 -->
<li>hi
<li>hello
<li>world
</ul>
<br/>
<!-- comment 2 -->
<script>
window.onload=findcommentsstart
var msgs = [];
function findcommentsstart()
{
    findcomments(document);
    var txt = document.createTextNode(msgs.join("\n"));
    document.getElementsByTagName("body")[0].appendChild(txt);
}

function findcomments(d)
{
    if(!d)
        return;
    if(d.nodeType==8)
        msgs.push("found a comment node " + d.data);
    if(!d.childNodes)
        return;
    for(var i=0;i<d.childNodes.length;i++)
        findcomments(d.childNodes[i]);
}
</script>
</body>
</html>

这篇关于如何获取javascript中注释元素的句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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