繁体   English   中英

调用JS函数onClick

[英]Calling JS function onClick

我正在尝试调用此Javascript函数。 当我按下按钮时,没有任何负载。 我为按钮设置了ID。 使用onclick时,我会加载onLinkedInLoad函数。 但是,不是我应该调用该函数来激活脚本的功能吗? 有人可以在这里看到我在做什么吗?

<!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8" />
    </head>
    <body>
        <button id="btn">Try Me</button>

        <script type="text/javascript">
            document.getElementById("btn").onclick = onLinkedInLoad;
        </script>

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

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

更新:我在控制台中得到了这个:

18 Uncaught ReferenceError: onLinkedInLoad is not defined
www.linkedin.com/uas/js/userspace?v=0.0.1191-RC8.56309-1429&apiKey=myKey&authorize=true&onLoad=onLinkedInLoad&secure=1&:
22 Uncaught Error: You must specify a valid JavaScript API Domain as part of this key's configuration.

我也放了APIkey。 我只是在这里用“ myKey”替换了代码。 我也尝试了.addEventListener('click', onLinkedInLoad) ,但仍然没有任何反应

尝试在调用脚本之前加载函数定义脚本。 或者,最好将所有脚本保存在另一个文件中,然后将其加载到HTML中。

我已经在js小提琴上尝试过您的代码,并且可以这样做。

暂无
暂无

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

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