繁体   English   中英

用EventListener调用一个函数

[英]call a function with EventListener

我真的很希望有人可以在这里帮助我。 我整个周末都在努力使代码正常工作,我向老板保证,它将一直工作到星期一早上。

更新

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <!--<script type="text/javascript" src="http://code.jquery.com/jquery-1.5b1.js"></script>-->
    <script type="text/javascript" src="http://platform.linkedin.com/in.js">
    api_key: my_API_key
    authorize: true
    onLoad: shareContent
    </script>

</head>
<body>

    <script type="in/Login"></script>

    <script>
            // Setup an event listener to make an API call once auth is complete
            function onLinkedInLoad() {
                IN.Event.on(IN, "auth", shareContent);
            }

            // Handle the successful return from the API call
            function onSuccess(data) {
                console.log(data);
            }

            // Handle an error response from the API call
            function onError(error) {
                console.log(error);
            }

            // Use the API call wrapper to share content on LinkedIn
            function shareContent() {

                // Build the JSON payload containing the content to be shared
                var payload = {
                    "comment": "Check out developer.linkedin.com! http://linkd.in/1FC2PyG",
                    "visibility": {
                        "code": "anyone"
                    }
                }

                IN.API.Raw("/people/~/shares?format=json")
                .method("POST")
                .body(JSON.stringify(payload))
                .result(onSuccess)
                .error(onError);
            }


    </script>

</body>
</html>

当我运行此代码时,将显示“使用linkedin登录”按钮。 当我单击此处时,我将登录LinkedIn。 登录后,我得到一个空白页,而在控制台中,我没有得到任何错误提示,只有:

但是如何使共享功能弹出?

在上方添加此脚本

<script type="text/javascript" src="http://platform.linkedin.com/in.js">
    api_key: My_API_Key_without_quotes
    onLoad:  Linkedin.init
</script>

并在此处移动您的api初始化

编辑(这仍然有错误,但现在应共享请求):

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />

<script type="text/javascript">
    function addListeners() {
        if(window.addEventListener) {
            document.getElementById('mybtn').addEventListener("click", shareContent, false);
        } else if(window.attachEvent) {
            document.getElementById('mybtn').attachEvent("onclick", shareContent);
        }
    }
    // Setup an event listener to make an API call once auth is complete
    function onLinkedInLoad() {
        IN.Event.on(IN, "auth", shareContent);
    }

    // Handle the successful return from the API call
    function onSuccess(data) {
        console.log(data);
    }

    // Handle an error response from the API call
    function onError(error) {
        console.log(error);
    }

    // Use the API call wrapper to share content on LinkedIn
    function shareContent() {

      // Build the JSON payload containing the content to be shared
      var payload = {
        "comment": "Check out developer.linkedin.com! http://linkd.in/1FC2PyG",
        "visibility": {
          "code": "anyone"
        }
      };

      IN.API.Raw("/people/~/shares?format=json")
        .method("POST")
        .body(JSON.stringify(payload))
        .result(onSuccess)
        .error(onError);
    }

    document.addEventListener("DOMContentLoaded", addListeners);

</script>

<script type="text/javascript" src="http://platform.linkedin.com/in.js">
    api_key: My_API_Key_without_quotes
    authorize: true
    onLoad:  onLinkedInLoad
</script>

</head>
<body>
    <button id="mybtn">Try Me</button>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM