簡體   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