我正在创建一个站点,该站点的检出菜单具有共享按钮,而不是检出按钮。他们赠送免费产品,并希望鼓励在社交媒体上分享。我正在尝试打开一个弹出窗口,将您带到每个站点各自的共享屏幕。我遇到的问题是只有一个链接有效,我单击Facebook按钮,然后会弹出twitter链接。
facebook = "http://www.facebook.com/sharer.php?s=100&p[title]=";
twitter = "https://twitter.com/intent/tweet?text=";
function popup() {
var twit = document.getElementById("twit");
var face = document.getElementById("face");
if (twit) {
var myWindow = window.open(window.location.href = twitter, "", "width=400, height=400");
}
}
/* else if (face){
window.open(window.location.href = facebook, "", "width=400, height=400");
}
}
*/
function popupFace() {
var face = document.getElementById("face");
if (face) {
var myFbWindow = window.open(window.location.href = facebook, "", "width=400, height=400");
}
}
<button id="twit" onclick="JaveScript:popup()">
twitter
</button>
<button id="face" onclick="JavaScript:popup()">
facebook
</button>
答案 0 :(得分:0)
除了可以解决您的问题的sinanspd的注释之外,我们还可以对此进行简化:
const facebook = "http://www.facebook.com/sharer.php?s=100&p[title]=";
const twitter = "https://twitter.com/intent/tweet?text=";
function popup(element) {
if (element.id === 'twit') {
window.open(window.location.href = twitter, "", "width=400, height=400");
} else if (element.id === "face") {
window.open(window.location.href = facebook, "", "width=400, height=400");
}
}
<button id="twit" onclick="popup(this)">
twitter
</button>
<button id="face" onclick="popup(this)">
facebook
</button>
如果这是仅有的两个将调用popup()
的按钮,则可以通过将facebook条件作为else子句来进一步简化它;在那里不需要比较。
请注意,此代码段有效,但在"Run code snippet"
窗口中无效。