gpt4 book ai didi

javascript - 调用JS函数onClick

转载 作者:行者123 更新时间:2023-12-03 06:54:35 24 4
gpt4 key购买 nike

我正在尝试调用这个 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 fiddle 上尝试过你的代码,它可以通过这样做来工作。

关于javascript - 调用JS函数onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37332528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com