jQuery转换onclick到onload

时间:2018-11-03 19:39:05

标签: javascript jquery

我有一个网站链接,当单击链接弹出窗口时,该链接当前正在工作,但是我希望onclick自动发生而无需单击。 onclick的工作方式应类似于onload

我尝试了很多Google搜索和StackOverflow搜索。.但是找不到确切的解决方案。

下面是我的代码:

<!DOCTYPE html>
<html>
<head>
    <title>mygame | Browser</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript" src="../api.js"></script>
    <script type="text/javascript">
        function initialize() {
            document.getElementById('robrowser').addEventListener("click", function(){
                var ROConfig = {
                    type:          ROBrowser.TYPE.POPUP,
                    application:   ROBrowser.APP.ONLINE,
                    remoteClient:  "http://play.mygame.com/client",
                    width:          1024,
                    height:         768,
                    development:    false,
                    servers: [{
                        display:     "mygame",
                        desc:        "mygame Revolution",
                        address:     "94.99.190.98",
                        port:        6900,
                        version:     46,
                        langtype:    12,
                        packetver:   20170614,
                        packetKeys:  false,
                        socketProxy: "ws://196.66.646.179:5999/"
                    }],
                    saveFiles:       true,
                    skipServerList:  true,
                    skipIntro:       true,
                    version:         1,
                    plugins: {
                        IntroMessage:  {}
                    }
                };
                var RO = new ROBrowser(ROConfig);
                RO.start();
            }, false );
        }
        window.addEventListener("load", initialize, false);
    </script>
</head>
<body>
    <input type="button" value="Run roBrowser" id="robrowser"/>
</body>

2 个答案:

答案 0 :(得分:1)

您可以像这样模拟按钮单击:

$("#robrowser").click();

在这种情况下,您要在加载页面时运行该方法。 您只需删除整个onclick侦听器,然后直接调用函数initialize()即可:

function initialize() {
                var ROConfig = {
                    type:          ROBrowser.TYPE.POPUP,
                    application:   ROBrowser.APP.ONLINE,
                    remoteClient:  "http://play.mygame.com/client",
                    width:          1024,
                    height:         768,
                    development:    false,
                    servers: [{
                        display:     "mygame",
                        desc:        "mygame Revolution",
                        address:     "94.99.190.98",
                        port:        6900,
                        version:     46,
                        langtype:    12,
                        packetver:   20170614,
                        packetKeys:  false,
                        socketProxy: "ws://196.66.646.179:5999/"
                    }],
                    saveFiles:       true,
                    skipServerList:  true,
                    skipIntro:       true,
                    version:         1,
                    plugins: {
                        IntroMessage:  {}
                    }
                };
                var RO = new ROBrowser(ROConfig);
                RO.start();
        }

initialize();

答案 1 :(得分:1)

进行了以下更改:

    点击事件监听器上的
  1. button已移至window加载函数
  2. 来自button click事件的源代码已移至initialize()
  3. button点击事件以功能initialize为目标
  4. window加载函数调用/执行initialize()

源代码:

<!DOCTYPE html>
<html>
<head>
<title>mygame | Browser</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="../api.js"></script>
<script type="text/javascript">
function initialize() {
    var ROConfig = {
        type:          ROBrowser.TYPE.POPUP,
        application:   ROBrowser.APP.ONLINE,
        remoteClient:  "http://play.mygame.com/client",
        width:          1024,
        height:         768,
        development:    false,
        servers: [{
display:     "mygame",
desc:        "mygame Revolution",
address:     "94.99.190.98",
port:        6900,
version:     46,
langtype:    12,
packetver:   20170614,
packetKeys:  false,
socketProxy: "ws://196.66.646.179:5999/"
        }],
        saveFiles:       true,
        skipServerList:  true,
        skipIntro:       true,
        version:         1,
        plugins: {
IntroMessage:  {}
        }
    };
    var RO = new ROBrowser(ROConfig);
    RO.start();      
}

window.addEventListener("load", function(){
document.getElementById('robrowser').addEventListener("click", initialize, false );
initialize();
}, false);
</script>
</head>
<body>
    <input type="button" value="Run roBrowser" id="robrowser"/>
</body>
</html>

如果对上面的源代码有任何疑问,请在下面留下评论,我会尽快与您联系。

希望这对您有所帮助,编码愉快!