jQuery 运行按钮点击页面加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18508262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:49:26  来源:igfitidea点击:

run button's click on page load

javascriptjqueryasp.net

提问by ozzy_mra

i have a input like this "

我有这样的输入“

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

that when i click on this , it runs a jQuery plugin. now i want to call this input's click event on my page load, in fact i want to run it's plugin at page load, so i use this code :

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

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

but when i run my page , i get this error :

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

Line: 16 Error: Object doesn't support property or method 'getElementById'

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

can enyone help me ,please?

请大家帮帮我好吗?

i done all of your answers but non of them worked for me!!! here in my page's code :

我做了你所有的答案,但没有一个对我有用!!!在我的页面代码中:

<%@ 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>

where am i do wrong?

我哪里做错了?

回答by Dhanesh Narvekar

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

or

或者

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

with id="osx" added to your input element

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

回答by Parfait

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

http://jsfiddle.net/rNGgm/

http://jsfiddle.net/rNGgm/

OR Without jQuery

或没有 jQuery

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

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

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

回答by sumer

Try this

尝试这个

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

回答by Shafi PS

use jQuerys .trigger()

使用 jQuery .trigger()

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

this is jQuery code..

这是jQuery代码..

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

回答by kondapaka

I think you place id in input type.

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

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

回答by Moazzam Khan

Why it did not work for you

为什么它不适合你

getElementByIdis not a method of window object. It is a method of documentobject, so use document.getElementById("osx").click();and your inputtag should have id='osx'.

getElementById不是 window 对象的方法。它是一个document对象的方法,所以使用document.getElementById("osx").click();和你的input标签应该有id='osx'.

you were using $("document"). Dont put quotes around documentin $(document).

你正在使用$("document"). 不要document$(document).

How to solve it

如何解决

Add idattribute to your input element -

id属性添加到您的输入元素 -

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

And if you like to use jQuery, then -

如果你喜欢使用 jQuery,那么 -

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

otherwise using document.getElementById

否则使用 document.getElementById

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

回答by Gautam3164

Try with .trigger()like

尝试.trigger()喜欢

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

And you didnt have the idatribute.So add it and try

而你没有id属性。所以添加它并尝试

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

回答by JDR

You can use "trigger":

您可以使用“触发器”:

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