document.getElementById(...)为null

时间:2016-05-25 16:06:06

标签: javascript html dom

我已经注意到关于这个主题的很多问题,但是,他们都提到<div>声明没有准备好。我的是。在单击按钮之前,不会调用document.getElementById()函数。 困扰我的是,我有3个document.getElementById()调用,它是中间问题。

我的页面....

    <?php
    (reference to session etc here)
    $user = (array_key_exists('LoggedInUser', $_SESSION)) ? $_SESSION['LoggedInUser'] : '';
    $HeaderLinks = "<A onclick='javascript:LoadPage(\"Admin\");' href='javascript:void(0);'>Home</A> | <A onclick='javascript:LoadPage(\"LogOut\");' href='javascript:void(0);'>Log out</A>"; // home and logout links

    $LoggedInUser = ($user == '') ? '&nbsp;': "Logged in as: <B>$user</B>";
    $HeaderLinks  = ($user == '') ? '&nbsp;': $HeaderLinks;
    ?>
        <BODY>
            <div class='main_disply_space center'> <!-- MAIN DISPLAY SPACE -->
    <!-- HEADER SECTION START -->
                <div class='page_header'> <!-- HEADER -->
                    <div class='title container'>
                        blah blah
                        <div class='subtitle contained'>
                            blah blah
                        </div>
                    </div>
                </div> <!-- HEADER end -->
                <div class='userandlinks'> <!-- USER and LINKS -->
                <div class='user container' id='LoggedInUser'>
                        <?php echo $LoggedInUser; ?>
                        <div class='links contained' id='AdminLinks'>
                            <?php echo $HeaderLinks; ?>
                        </div>
                    </div>
                </div> <!-- USER and LINKS end -->
    <!-- HEADER SECTION END -->
    <!-- BODY SECTION START -->
                <div class=body id='ActivePage'> <!-- DISPLAY AREA -->
                &nbsp;
                </div> <!-- DISPLAY AREA end -->
    <!-- BODY SECTION END -->
    <!-- FOOTER SECTION START -->
                <div class='footerlinks' id='FooterLinks'> <!-- LINKS -->
                    <?php echo $HeaderLinks; ?>
                </div> <!-- LINKS end -->
                <div class='copyright'> <!-- COPYRIGHT -->
                    more blah blah
                </div> <!-- COPYRIGHT end -->
    <!-- FOOTER SECTION END -->
            </div> <!-- MAIN DISPLAY SPACE end -->
        </BODY>

javascript ...

        send = "username=" + user + "&password=" + pwd;
        if(btn === 'Add')
        {
            if(RunPHPFile('AdminUserAdd', send) === '1')
                if(RunPHPFile('UserLogin', send) === '1')
                {
                    // Show who's logged in
                    document.getElementById("LoggedInUser").innerHTML = 'Logged in as: <B>'+user+'</B>';

                    // show home and logout links
                    HLinks = "<A onclick='javascript:LoadPage(\"Admin\");' href='javascript:void(0);'>Home</A> | <A onclick='javascript:LoadPage(\"LogOut\");' href='javascript:void(0);'>Log out</A>";
                    document.getElementById('AdminLinks').innerHTML = HLinks;
                    document.getElementById('FooterLinks').innerHTML = HLinks;
                    LoadPage('Admin');
                }
                else
                    document.getElementById('error').innerHTML = 'Error logging in new user.';
            else
                document.getElementById('error').innerHTML = 'Error adding new user.';
        }

我在document.getElementById()上使用了几个try {} catch(){}来查找它无法正常工作的原因。

继承人的困惑。在javascript中,你可以看到3个document.getElementById()的一个接一个,'LoggedInUser','AdminLinks'和&amp; 'FooterLinks'。在HTML中,您可以看到相应的id =''。 现在,我的问题是'LoggedInUser'和'FooterLinks'都可以正常工作,但'AdminLinks'会引发'is null'错误。我一遍又一遍地编码,我看不出问题所在。

1 个答案:

答案 0 :(得分:3)

您正在替换SELECT H.vehicle_code, G.group_name, cast(Count(DISTINCT D.cusip_sedol) as DECIMAL)/cast(count(DISTINCT G.group_name) as DECIMAL) AS Total --add this second part FROM tbltrading_holdings AS H INNER JOIN tbltrading_stocks_data_stocks AS D ON H.cusip_sedol = D.cusip_sedol LEFT JOIN tbltrading_gic AS G ON D.gic_code = G.gic_code WHERE vehicle_code IN (SELECT vehicle_code FROM tbltrading_vehicles WHERE vehicle_name LIKE 'J%') AND D.gic_code IS NOT NULL GROUP BY H.vehicle_code, G.group_name ORDER BY vehicle_code 的内容。猜猜#LoggedInUser div中有什么?那是对的 - #LoggedInUser。所以#AdminLinks返回null,因为 - 页面上没有带有该id的元素。