如何在页面加载页面后自动单击“共享按钮”。我已经尝试过第一种方法,但仍然不能自动单击。第二条是原始代码。目前仍然需要我手动单击共享按钮。
1.
这一个使用"window.onload“方法。
<div>
<button id="share-button" onclick="autoClick()">Button</button>
</div>
</div>函数autoClick() { FB.ui({ FB.ui:‘弹出“,方法:'feed',链接:'https://developers.facebook.com/docs/',
}, function (response) {
//Debug response (optional)
console.log(response);
});
}
window.onload = function () {
document.getElementById('share-button').click();
}<div id="buttons">
<div>
<button type="button" id="share-button">Continue</button>
</div>
</div>
<script>
window.fbAsyncInit = function () {
FB.init({
appId: 'ID',
autoLogAppEvents: true,
xfbml: true,
version: 'v11.0'
});
};
document.getElementById('share-button').addEventListener('click', function () {
FB.ui({
display: 'popup',
method: 'share',
href: 'https://developers.facebook.com/docs/',
}, function (response) {
//Debug response (optional)
console.log(response);
});
});
//Load the JavaScript SDK asynchronously
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementsById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js"></script>发布于 2021-09-14 19:33:27
这种做法是错误的。与其“单击”一个按钮,不如在一个单独的函数中编写侦听器,然后调用该函数。
类似于:
document.getElementById('share-button').addEventListener('click', share);
function share() {
FB.ui({
display: 'popup',
method: 'share',
href: 'https://developers.facebook.com/docs/',
}, function (response) {
//Debug response (optional)
console.log(response);
});
}这样你就可以在任何时候做这样的事情:
window.onload = function () {
share();
}https://stackoverflow.com/questions/69183535
复制相似问题