getElementsByClassName IE解析问题
我在弄清楚如何解决 IE 中的 getElementsByClassName 问题时遇到问题。我如何最好地在我的代码中实现罗伯特·尼曼(无法发布链接,因为我的代表只有 1)分辨率?或者 jquery 解析会更好吗?我的代码是
function showDesc(name) {
var e = document.getElementById(name);
//Get a list of elements that have a class name of service selected
var list = document.getElementsByClassName("description show");
//Loop through those items
for (var i = 0; i < list.length; ++i) {
//Reset all class names to description
list[i].className = "description";
}
if (e.className == "description"){
//Set the css class for the clicked element
e.className += " show";
}
else{
if (e.className == "description show"){
return;
}
}}
并且我在此页面上使用它 dev.msmnet.com/services/practice-management 显示/隐藏每个服务的描述(适用于 Chrome 和 FF)。任何提示将不胜感激。
I am having issues figuring out how to resolve the getElementsByClassName issue in IE. How would I best implement the robert nyman (can't post the link to it since my rep is only 1) resolution into my code? Or would a jquery resolution be better? my code is
function showDesc(name) {
var e = document.getElementById(name);
//Get a list of elements that have a class name of service selected
var list = document.getElementsByClassName("description show");
//Loop through those items
for (var i = 0; i < list.length; ++i) {
//Reset all class names to description
list[i].className = "description";
}
if (e.className == "description"){
//Set the css class for the clicked element
e.className += " show";
}
else{
if (e.className == "description show"){
return;
}
}}
and I am using it on this page dev.msmnet.com/services/practice-management to show/hide the description for each service (works in Chrome and FF). Any tips would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我很好奇你的函数的 jQuery 版本会是什么样子,所以我想出了这个:
I was curious to see what a jQuery version of your function would look like, so I came up with this:
这应该支持多个类。
您也可以传入父级,以使其搜索 DOM 更快一些。
如果您希望
getElementsByClassName('a c')
匹配 HTML然后尝试像这样更改它...
如果您如果希望此函数仅在尚不存在时才可用,请将其定义包装为
This should support multiple classes.
You can pass in a parent too, to make its searching the DOM a bit faster.
If you want
getElementsByClassName('a c')
to match HTML<div class="a b c" />
then try changing it like so...If you want this function to only be available if it doesn't already exist, wrap its definition with
更简单的 jQuery 解决方案:
如果您使用 jQuery,为什么还要费心使用
show
类呢?Even easier jQuery solution:
Why bother with a
show
class if you are using jQuery?这是我整理的一个,可靠而且可能是最快的。应该在任何情况下都能工作。
Heres one I put together, reliable and possibly the fastest. Should work in any situation.
我曾经实现 HTMLElement.getElementByClassName(),但至少 Firefox 和 Chrome,当这些元素很多时只能找到一半的元素,而不是我使用类似的东西(实际上它是一个更大的函数):
我认为会有无需 XPATH 进行迭代的更快方法,因为 RegExp 很慢(可能是带有 .indexOf 的函数,应该对其进行测试),但它运行良好
I used to implement HTMLElement.getElementByClassName(), but at least Firefox and Chrome, only find the half of the elements when those elements are a lot, instead I use something like (actually it is a larger function):
I think there would be a faster way to iterate without XPATH, because RegExp are slow (perhaps a function with .indexOf, it shuld be tested), but it is working well
您可以将
getElementsByClassName()
替换为以下内容:然后您可以像这样使用它:
You can replace
getElementsByClassName()
with the following:Then you can use it like this: