0

如果内容不同,此 JavaScript 代码用于更改 div 的背景:

function myfunction() {
    $a = document.getElementById('t').getElementsByTagName('div');
    var x = 0;
    while(x < $a.length) {
        var b = ($a[x].innerHTML);
        b.toString;
        alert(x + b[x]);
        if(b == 'PENDING SELECTING TEAM MEMBER' ||b == 'PENDING APPROVAL BY SCHEME MANAGER' ||b == 'PENDING SELECTING TEAM MEMBER' ||b == 'PENDING REVIEW TEAM MEMBER BY LEAD ASSESSOR' ||b == 'PENDING ACKNOWLEDGEMENT BY TA/LA' ||b == 'PENDING PREPARE AUDIT PLAN BY LA' ||b == 'PENDING REVIEW AUDIT PLAN BY AO' ||b == 'PENDING ACCEPT LA BY LAB' ||b == 'PENDING VERIFY REASON BY AO' ||b == 'PENDING CONDUCT ASSESSMENT' || b == 'PENDING CORRECTIVE ACTION BY CAB' || b == 'PENDING REVIEW BY AO' || b == 'PENDING CORRECTIVE ACTION APPROVAL BY SCHEME MANAGER' ||b == 'PENDING APPROVAL BY DOA' ){
            document.getElementById('status'+x).style.background="maroon";
            alert(x + b);
        }
        x++;

    }
}

它不起作用,因为if语句失败。

有人可以帮我找出问题所在吗?

4

3 回答 3

2

就像一个注释,b.toString;应该是b.toString();

而且我个人更喜欢使用双引号,但这只是因为习惯于强输入它们并且在字符和字符串之间存在差异。

看到其他人这么说,但只是为了强调:函数中没有'var'关键字的变量使它们成为全局变量,这意味着它们是可访问的,并且可以在程序的任何范围内更改。

尝试使用以下 javascript 代码:JSFiddle

更改了设置背景颜色的方式。

于 2012-11-01T23:58:58.037 回答
0

考虑使用_.containsfrom underscore.js,这会使if复杂度降低一些。相反,它看起来更像:

var collection = [
    'PENDING SELECTING TEAM MEMBER', 
    'PENDING APPROVAL BY SCHEME MANAGER', 
    'PENDING SELECTING TEAM MEMBER', 
    'PENDING REVIEW TEAM MEMBER BY LEAD ASSESSOR', 
    'PENDING ACKNOWLEDGEMENT BY TA/LA', 
    'PENDING PREPARE AUDIT PLAN BY LA', 
    'PENDING REVIEW AUDIT PLAN BY AO', 
    'PENDING ACCEPT LA BY LAB', 
    'PENDING VERIFY REASON BY AO', 
    'PENDING CONDUCT ASSESSMENT', 
    'PENDING CORRECTIVE ACTION BY CAB', 
    'PENDING REVIEW BY AO', 
    'PENDING CORRECTIVE ACTION APPROVAL BY SCHEME MANAGER', 
    'PENDING APPROVAL BY DOA'];

if(_.contains(collection, b)) {
    alert("this condition is true!");
    //...
}

如果它发出警报,则它是 if 语句中的问题。如果不是,则不满足条件,我可能会先查看 HTML 中的前导和尾随空格。

于 2012-11-02T00:23:33.703 回答
0

您可以通过简化 if 语句来“测试”它(这是一种通用调试技术):

var b = 'PENDING SELECTING TEAM MEMBER';

if (b == 'PENDING SELECTING TEAM MEMBER' || b == 'PENDING APPROVAL BY SCHEME MANAGER' || b == 'PENDING SELECTING TEAM MEMBER' || b == 'PENDING REVIEW TEAM MEMBER BY LEAD ASSESSOR' || b == 'PENDING ACKNOWLEDGEMENT BY TA/LA' || b == 'PENDING PREPARE AUDIT PLAN BY LA' || b == 'PENDING REVIEW AUDIT PLAN BY AO' || b == 'PENDING ACCEPT LA BY LAB' || b == 'PENDING VERIFY REASON BY AO' || b == 'PENDING CONDUCT ASSESSMENT' || b == 'PENDING CORRECTIVE ACTION BY CAB' || b == 'PENDING REVIEW BY AO' || b == 'PENDING CORRECTIVE ACTION APPROVAL BY SCHEME MANAGER' || b == 'PENDING APPROVAL BY DOA')
{
    alert("Working");
}
于 2012-11-02T00:49:28.380 回答