Javascript自动点击无法正常工作

时间:2012-12-18 06:57:22

标签: javascript

我正在使用脚本在html中对表进行排序。脚本在这里 - http://www.kryogenix.org/code/browser/sorttable/。我希望在加载时自动点击对html表进行排序的文本。我正在使用的autoclick脚本就是这个 -

<head>
<script LANGUAGE='javascript'>
function autoClick(){
document.getElementById('sort').click();
}
</script>
</head>
<body onload="autoClick();">
<table><tr><th><p id="sort">Click here to sort the table</p></th>...

问题是这不起作用,我很困惑,为什么这不起作用。

-------------------- EDIT ------------------

对不起,但实际上我在正文onload语句中键入了错误。因此我使用的脚本是正确的。

3 个答案:

答案 0 :(得分:1)

您在哪里定义了活动?
因为我在你的onload中看到了一个函数。

下面,一个很好的例子:

<html>
    <head>
        <script type='text/javascript'>
        var init = function() 
        {
            document.getElementById('test').addEventListener('click', function() {
                alert('Auto test is ok');
            }, false);
        };

        function autoClick(){
            document.getElementById('test').click();
        }

        </script>
    </head>
    <body onload="init(); autoClick();">
        <button id="test">Test</button>
    </body>
</html>

答案 1 :(得分:0)

使用Jquery库总是更安全。只需在页面的标题部分包含最新的Jquery库:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js" type="text/javascript"></script>

并且此功能应解决您的问题:

$("#sort").live('click');

答案 2 :(得分:0)

我认为你错过了表格中的班级名称

请添加名为“sortable”的类名称的表标记。