Jquery attr('id')不起作用

时间:2011-02-26 20:48:36

标签: jquery alert attr

<!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”!!!! 我不知道这里到底发生了什么。

2 个答案:

答案 0 :(得分:3)

您必须将<{1}}函数传递onLoad不能调用

ready

否则,您会将$(document).ready(onLoad); // no parenthesis ----^ 返回值传递给onLoad方法,即调用ready并在DOM准备就绪之前。

答案 1 :(得分:0)

您在就绪活动中使用onLoad返回值

这意味着onLoad函数立即被称为 (以找出returnv alue是什么)并且您正在寻找的元素不存在(因为DOM的那个)元素尚未构建那个时间)。

关闭()以传递函数而不是其返回值。

相关问题