当元素ID包含美元符号时,Javascript中的getElementByid [英] getElementByid in Javascript when element ID contains Dollar sign
问题描述
getElementById()似乎没有找到该元素。在纯Javascript中有没有办法解决这个问题?我无法控制元素ID,因为它们是由页面构建器生成的,我不能在此应用程序中使用jQuery。
getElementById() does not appear to find the element when the ID contains a dollar sign. Is there a way around this in pure Javascript? I have no control over the element ID's as these are generated by a "page builder" and I cannot use jQuery in this application.
示例:
<div id="win0divFRED$0">
var orig = document.getElementById("win0divFRED$0");
非常感谢,
John
Many Thanks,
John
推荐答案
由于HTML4.01规范相当严格
Since the HTML4.01 spec is pretty restrictive
ID和NAME令牌必须以字母([A-Za-z])开头,后面可以跟任意数量的字母,数字([0-9]),连字符( - ),下划线(_),冒号( :)和句点(。)。
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
根据4.01规范,您的ID无效。
与HTML5相比:
your id would be invalid according to 4.01 spec.
In contrast to HTML5:
该值必须在元素的主子树中的所有ID中唯一,且必须至少包含一个字符。该值不得包含任何空格字符。
The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.
我认为它可能是doctype的问题。尝试:
I think it might be a problem with the doctype. Try:
<!doctype html>
这篇关于当元素ID包含美元符号时,Javascript中的getElementByid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!