创建弹出窗口按钮

洛伦佐·史密斯

我正在创建一个站点,其中结帐菜单具有共享按钮而不是结帐按钮。他们正在赠送免费产品,并希望鼓励在社交媒体上分享。我正在尝试打开一个弹出窗口,将您带到每个站点各自的共享屏幕。我遇到的问题是只有一个链接有效,我点击 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>

迈克尔·罗德里格斯

添加到 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"窗口内。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章