页面加载时运行按钮的点击

4
我有一个像这样的输入:“

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

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

<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个回答

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

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

在您的输入元素中添加id="osx"。

我再次收到相同的错误信息: "Line: 16 Error: Unable to get property 'click' of undefined or null referen"。 - ozzy_mra
你是否在输入标签中添加了id属性? - Dhanesh Narvekar
第一部分不正确,因为您使用了 $("document"),而这应该没有引号。 - Moazzam Khan

5
<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/


我再次遇到了这个错误:"行:12 错误:无法获取未定义或空引用的属性'click'" - ozzy_mra
移除 $("document") 中的引号。 - Moazzam Khan

1

试试这个

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

0

为什么它对你不起作用

getElementById 不是 window 对象的方法。它是 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();
}

你之前使用的是 $("document")。在 $(document) 中不要加引号。 - Moazzam Khan

0

尝试使用.trigger(),例如

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

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

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

0
您可以使用“触发器”:
 <script>
     $(function () {
         $(".osx").trigger("click");
     });
 </script>

我使用了那个,但是没有任何反应。 - ozzy_mra
奇怪。控制台说了什么? - JDR

0
使用jQuery的.trigger()函数。
<input type='button' id='osx' value='Demo' class='osx demo' runat="server" /> 

这是 jQuery 代码...

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

0

我认为你把id放在了input type里面。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接