How do I get the handle to a comment element in javascript?
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$ctl00$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>
You could avoid this by loading the page in a String variable (e.g. using AJAX) and parsing it.
I would say ignoring comments should be the desired behaviour, beacuse comments should be considered a "no-go" for the parser/interpreter.
IE is always doing some fancy (non-standard) stuff which often breaks things ;)
精彩评论