English 中文(简体)
印章模块操作
原标题:Javascript modulo operation in IE7

为了在名单上的每一个第三个项目增加一个类别,我正在采取类似行动:

var projectElements = document.getElementById("projectList").children;
var iCount = 0;
for (var i in projectElements) {
    iCount++;
    if (iCount % 3 == 0) {
        if (projectElements[i].className == "") {
            projectElements[i].className += "projectLinkLast";
        }
        else {
            projectElements[i].className += " projectLinkLast";
        }
    }
}

It works fine in Firefox and other browsers except for Internet Explorer 7. Does anybody have a clue why?

最佳回答

问题在于模块操作者。 问题可能是你在东道物体上使用for.in(在这种情况下,children of property of an elements),这是一个坏的主意,不能保证按您的期望(或实际上)工作。 此外,<儿童编码>。 所有浏览器都未获得支持(尽管在7年国际电子计算法中),因此无法解决该浏览器的问题。 我建议如下:

var iCount = 0, child = document.getElementById("projectList").firstChild;
while (child) {
    if (child.nodeType == 1) { // Only deal with elements
        iCount++;
        if (iCount % 3 == 0) {
            if (child.className == "") {
                child.className = "projectLinkLast";
            }
            else {
                child.className += " projectLinkLast";
            }
        }
    }
    child = child.nextSibling;
}
问题回答

Try using the .setAttribute ("}Name”,“blah” und the .className.

Got a demo of your code on JSFiddle and seems to be working: http://jsfiddle.net/LWsTn/6/

如果是gon,则使用 j。 使用:

$("#projectList :nth-child(3n").addClass("projectLinkLast");

Documentation: http://api.jquery.com/nth-child-selector/

www.un.org/Depts/DGACM/index_spanish.htm 添加集束器法j Query正在使用,可能有助于。

classNames = value.split(rspace);

for (i = 0, l = this.length; i < l; i++) {
    elem = this[i];

    if (elem.nodeType === 1) {
        if (!elem.className && classNames.length === 1) {
            elem.className = value;

        } else {
            setClass = " " + elem.className + " ";

            for (c = 0, cl = classNames.length; c < cl; c++) {
                if (!~setClass.indexOf(" " + classNames[c] + " ")) {
                    setClass += classNames[c] + " ";
                }
            }
            elem.className = jQuery.trim(setClass);
        }
    }
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签