开发者

Create a classList substitute for the browsers that haven't implemented it yet

There's a question that looks like this one here:

Create my own classList object when the browser does not impleme开发者_StackOverflow社区nt it itself

The problem is that the answer works in some browsers but does not work in IE7 and in flock (at least).

So I'd like to have an alternative that works with these browsers.

What I want is simple but I believe it's not simple to implement.

I want to be able to apply this code to any browser build from the time that IE7 or FF 3.0 was build:

var select = document.createElement('select');
select.classList.add('guestSelect');

How can I accomplish that?

Note that I don't want to use any frameworks or any libraries. I want to be the one to write the code!

I'd like to do it this way specially because I want to learn how to accomplish these kinds of things.


Maybe you don't need this answer anymore, but other may still be looking for it.

Since IE7 doesn't implement the Element class, you can't extend its prototype. The best you can do is to create your own element class that wraps DOM elements, and use it everywhere. That's what most frameworks do, by the way.

Mind you, this could dramatically change the code you've already written. That's why it's better (simpler?) to rely on global functions like addClass(element, "foo");.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜