最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Using colon in html tag and handle its element in javascript - Stack Overflow

programmeradmin0浏览0评论

why my "myns:button" don't bee red in IE 6 / 7 / 8 unlike in 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 :

document.createElement('myns:button');

But that doesn't work in IE, why ? Thanks.

why my "myns:button" don't bee red in IE 6 / 7 / 8 unlike in 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 :

document.createElement('myns:button');

But that doesn't work in IE, why ? Thanks.

Share Improve this question edited Jan 20, 2023 at 13:58 aynber 23k9 gold badges54 silver badges68 bronze badges asked May 30, 2010 at 9:02 bernedefbernedef 7072 gold badges12 silver badges22 bronze badges 2
  • What is a myns:button? More importantly, how would the browser know what it is? – John Saunders Commented May 30, 2010 at 9:05
  • is there a DTD added to this document? see htmlhelp./tools/validator/customdtd.html - it's not remended to use custom DTDs according to w3/Style/customdtd tho – cryo Commented May 30, 2010 at 9:08
Add a ment  | 

3 Answers 3

Reset to default 2

Like the others I really remend 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./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./namespaces for your own namespace name.

Tested in latest versions of FF, Chrome, Opera and in IE6, IE7 and IE8.

See http://msdn.microsoft./en-us/library/ms534388%28VS.85%29.aspx as a starting place for information about how this works in IE.

myns:button is not a valid HTML tag and browsers might interpret it differently.

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 mon HTML way would be to use a class attribute and style the button with 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.

发布评论

评论列表(0)

  1. 暂无评论