我正在尝试集成Yammer共享按钮https://developer.yammer.com/docs/share-button,我按照指示成功实现,但唯一的问题是第一次需要两次点击才能启动,稍后单击似乎就可以完成工作。这是下面的代码。
function clickSaveShare(){
var options = {
customButton : true, //false by default. Pass true if you are providing your own button to trigger the share popup
classSelector: 'homeBtn',//if customButton is true, you must pass the css class name of your button (so we can bind the click event for you)
defaultMessage: 'My custom Message', //optionally pass a message to prepopulate your post
pageUrl: 'www.microsoft.com' //current browser url is used by default. You can pass your own url if you want to generate the OG object from a different URL.
};
yam.platform.yammerShare(options);
}

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<span href="#" class="homeBtn" onclick="clickSaveShare(339,'Reverse KT')"> Click here to share</a>
<script>
</script>
<script type="text/javascript" src="https://s0.assets-yammer.com/assets/platform_social_buttons.min.js"></script>
<script type="text/javascript">yam.platform.yammerShare();</script>
</body>
</html>
&#13;
答案 0 :(得分:0)
调用yam.platform.yammerShare()
实际上不会调用共享给Yammer函数,尽管它的名称如此。它的作用是将click事件应用于指定的DOM元素,以便在单击该元素时,将显示Yammer弹出窗口。
您必须单击两次按钮的原因是,第一次调用clickSaveShare
时,它将调用yam.platform.yammerShare()
,这将在指定的DOM元素上设置单击事件。下次单击该按钮时,您的点击事件已被Yammer替换,因此可以正常工作。
鉴于要包含jQuery,一种简单的解决方法是使用jQuery的document.ready事件:
$(document).ready(function() {
var options = {
customButton : true,
classSelector: 'homeBtn',
defaultMessage: 'My custom Message',
pageUrl: 'www.microsoft.com'
};
yam.platform.yammerShare(options);
});
Here是上述的CodePen示例。