2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(onLoad());

        function onLoad() {
            alert($("#wcontrol_subtable0").attr('id'));
        }
    </script>
    <title></title>
</head>
<body>
    <form name="form1" method="post" action="Default.aspx" id="form1">
    <div id="wcontrol_pnlMenu">
        <table border="0">
            <tr>
                <td>
                    <table id="wcontrol_subtable0" class="class1" cellspacing="0" cellpadding="0" border="0"
                        style="border-collapse: collapse;">
                        <tr id="wcontrol_subtable0_th">
                            <th>
                                Parameters
                            </th>
                        </tr>
                        <tr>
                            <td>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

看起来很正常,问题是警报方法出现“null”!!!!我不知道这里到底发生了什么。

4

2 回答 2

3

您必须将函数传递给,而不是调用它:onLoadready

$(document).ready(onLoad);
//  no parenthesis ----^

否则,您会将 的返回值传递onLoadready方法,即onLoad在 DOM 准备好之前被调用。

于 2011-02-26T20:51:03.247 回答
0

您正在使用就绪事件中的返回值。onLoad

这意味着该onLoad函数被立即调用(以找出返回值是什么)并且您要查找的元素不存在(因为那个时候该元素的 DOM 尚未构建)。

取消()传递函数而不是其返回值。

于 2011-02-26T20:51:47.403 回答