[英]Using colon in html tag and handle its element in javascript
why my "myns:button" don't become red in IE 6 / 7 / 8 unlike in Firefox / Opera / Safari / Chrome ? 为什么我的“myns:button”在IE 6/7/8中不会变成红色,这与Firefox / Opera / Safari / Chrome不同?
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var tmp = document.getElementsByTagName('myns:button');
for (i = 0; i < tmp.length; i++) {
tmp[i].style.color = '#FF0000';
}
};
</script>
</head>
<body>
<myns:button>My NS Button</myns:button>
</body>
</html>
I already tried to prepend the following to my js : 我已经尝试将以下内容添加到我的js中:
document.createElement('myns:button');
But that doesn't work in IE, why ? 但这在IE中不起作用,为什么呢? Thanks.
谢谢。
Like the others I really recommend that you don't do this. 像其他人一样,我真的建议你不要这样做。 But ...
但......
If you really want to, and you're not too concerned about validation you can do: 如果您真的想要,并且您不太关心验证,您可以这样做:
<html xmlns:myns='http://www.example.com/namespaces'>
<head>
<script type="text/javascript">
window.onload = function() {
var tmp = document.getElementsByTagName(
(typeof document.body.scopeName == "undefined") ?
'myns:button' : 'button');
for (i = 0; i < tmp.length; i++) {
if ((typeof tmp[i].scopeName == "undefined") ||
(tmp[i].scopeName == 'myns'))
tmp[i].style.color = '#FF0000';
}
};
</script>
</head>
<body>
<myns:button>My NS Button</myns:button>
</body>
</html>
Change http://www.example.com/namespaces for your own namespace name. 更改http://www.example.com/namespaces以获取您自己的命名空间名称。
Tested in latest versions of FF, Chrome, Opera and in IE6, IE7 and IE8. 在最新版本的FF,Chrome,Opera以及IE6,IE7和IE8中进行了测试。
See http://msdn.microsoft.com/en-us/library/ms534388%28VS.85%29.aspx as a starting place for information about how this works in IE. 请参阅http://msdn.microsoft.com/en-us/library/ms534388%28VS.85%29.aspx作为有关其在IE中如何工作的信息的起始位置。
myns:button
不是有效的HTML标记,浏览器可能会以不同的方式解释它。
I'm not sure why the need for a non-standard DOM element, but if the requirement is to make a button with a different look (red text), the common HTML way would be to use a class attribute and style the button with CSS : 我不确定为什么需要一个非标准的DOM元素,但如果要求制作一个具有不同外观(红色文本)的按钮,常见的HTML方式是使用一个类属性并设置按钮样式CSS :
<style type="text/css">
.mynsbutton {color: #ff0000;}
</style>
<input type="button" class="mynsbutton" value="My Button" />
If you then need to find all your mynsbutton-elements with javascript for some more processing, you could look into the jQuery -library, or use one of the ready snippets of code on the internet to find all elements with your mynsbutton-class. 如果您需要使用javascript查找所有mynsbutton元素以进行更多处理,您可以查看jQuery -library,或者使用Internet上的一个现成代码片段来查找mynsbutton-class的所有元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.