运行按钮点击页面加载

时间:2013-08-29 10:18:18

标签: javascript jquery asp.net

我有这样的输入“

<input type='button' name='osx' value='Demo' class='osx demo' runat="server" />

当我点击它时,它会运行一个jQuery插件。 现在我想在我的页面加载上调用此输入的单击事件,实际上我想在页面加载时运行它的插件,所以我使用此代码:

<script>
    $("document").ready(function () {
        window.getElementById("osx").click();
    });
</script>

但是当我运行我的页面时,我收到此错误:

  

行:16   错误:对象不支持属性或方法'getElementById'

能帮助我吗?

我完成了你所有的答案,但不是他们为我工作! 在我的页面代码中:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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 runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <link type='text/css' href='css/osx.css' rel='stylesheet' media='screen' />
    <script>
       $("document").ready(function () {
   document.getElementById("osx").click();
}
</script>
</head>
<body>
    <div id='container'>
        <div id='content'>
            <div id='osx-modal'>
                <input id='osx' type='button' name='osx' value='Demo' class='osx demo' runat="server" />
                or <a href='#' class='osx'>Demo</a>
            </div>
            <!-- modal content -->
            <div id="osx-modal-content">
                <div id="osx-modal-title" dir="rtl">
                    OSX Style Modal Dialog</div>
                <div class="close">
                    <a href="#" class="simplemodal-close">x</a></div>
                <div id="osx-modal-data">
                    <h2>
                        Hello! I'm SimpleModal!</h2>
                    <p>
                        SimpleModal is a lightweight jQuery Plugin which provides a powerful interface for
                        modal dialog development. Think of it as a modal dialog framework.</p>
                    <p>
                        SimpleModal gives you the flexibility to build whatever you can envision, while
                        shielding you from related cross-browser issues inherent with UI development..</p>
                    <p>
                        As you can see by this example, SimpleModal can be easily configured to behave like
                        an OSX dialog. With a handful options, 2 custom callbacks and some styling, you
                        have a visually appealing dialog that is ready to use!</p>
                    <p>
                        <button class="simplemodal-close">
                            Close</button>
                        <span>(or press ESC or click the overlay)</span></p>
                </div>
            </div>
        </div>
    </div>
    <script type='text/javascript' src='js/jquery.simplemodal.js'></script>
    <script type='text/javascript' src='js/osx.js'></script>
</body>
</html>

我在哪里做错了?

8 个答案:

答案 0 :(得分:6)

<script>
$(document).ready(function () {
   document.getElementById("osx").click();
});
</script>

<body onload="document.getElementById('osx').click();">

将id =“osx”添加到您的输入元素

答案 1 :(得分:3)

<script>
    $(document).ready(function () {
        $("input[name=osx]").click();
    });
</script>

http://jsfiddle.net/rNGgm/

或没有jQuery

<script>
    document.addEventListener('DOMContentLoaded',function(){
        document.getElementById("osx").click();
    });
</script>

http://jsfiddle.net/rNGgm/1/

答案 2 :(得分:1)

试试这个

$(document).ready(function () {
    document.getElementById("<%=btnProvideContactInfo.ClientID%>").click();
});

答案 3 :(得分:0)

为什么它不起作用

getElementById不是窗口对象的方法。这是document对象的方法,因此请使用document.getElementById("osx").click();input标记应为id='osx'

您使用的是$("document")。不要在document中的$(document)附近加上引号。

如何解决

id属性添加到输入元素 -

<input id='osx' type='button' name='osx' value='Demo' class='osx demo' runat="server" />

如果你想使用jQuery,那么 -

 $(document).ready(function () {    
    $("#osx").click();//using id selector
 }

否则使用document.getElementById

$(document).ready(function () {
   document.getElementById("osx").click();
}

答案 4 :(得分:0)

尝试.trigger()喜欢

<script type="text/javascript">
    $("document").ready(function () {
        $(".osx").trigger('click');
    });
</script>

你没有id atribute.So添加它并尝试

<input type='button' name='osx' value='Demo' class='osx demo' id='osx' runat="server" />

答案 5 :(得分:0)

您可以使用“触发器”:

 <script>
     $(function () {
         $(".osx").trigger("click");
     });
 </script>

答案 6 :(得分:0)

使用jQuerys .trigger()

<input type='button' id='osx' value='Demo' class='osx demo' runat="server" /> 

这是jQuery代码..

<script>
    $("document").ready(function () {
        $('#osx').trigger('click');
    });
</script>

答案 7 :(得分:0)

我认为你将id放在输入类型中。

<input type='button' name='osx' value='Demo' class='osx demo' id='osx' runat="server" />