【问题标题】:Onclick to show content and target="_blank" another link [closed]Onclick 以显示内容和 target="_blank" 另一个链接 [关闭]
【发布时间】:2015-01-20 02:44:51
【问题描述】:

我需要一个按钮 onclick 来显示内容,同时它也将 target="_blank" 一个新窗口...有可能吗? 我尝试了一些代码但无法正常工作,有人可以帮我解决这个问题吗? 谢谢大家!

<style type="text/css">
   /* This CSS is used for the Show/Hide functionality. */
   .more {
      display: none;
      border-top: 1px solid #666;
      border-bottom: 1px solid #666; }
   a.showLink, a.hideLink {
      text-decoration: none;
      color: #36f;
      padding-left: 8px;
      background: transparent url(down.gif) no-repeat left; }
   a.hideLink {
      background: transparent url(up.gif) no-repeat left; }
   a.showLink:hover, a.hideLink:hover {
      border-bottom: 1px dotted #36f; }
</style>

<div id="wrap">
<h1>Show/Hide Content</h1>
<a href="https://www.facebook.com/sharer/sharer.php?app_id=309437425817038&sdk=joey&u=http%3A%2F%2Fcloudsblack.com%2F180115f.html&display=popup&ref=plugin"
id="example-show" class="showLink" onclick="showHide('example');return false;">
<button class="btn-u btn-u-lg btn-block btn-u-dark-blue">
<i class="fa fa-facebook"></i> Facebook
</button>
</a>
<div id="example" class="more">
<p>Congratulations! You've found the magic hidden text! Clicking the link below will hide this content again.</p>
<p><a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;">Hide this content.</a></p>
</div>
</div>                  

<!-- Hide Function -->           
<script language="javascript" type="text/javascript">
function showHide(shID) {
   if (document.getElementById(shID)) {
      if (document.getElementById(shID+'-show').style.display != 'none') {
         document.getElementById(shID+'-show').style.display = 'none';
         document.getElementById(shID).style.display = 'block';
      }
      else {
         document.getElementById(shID+'-show').style.display = 'inline';
         document.getElementById(shID).style.display = 'none';
      }
   }
}
</script>

【问题讨论】:

  • 为什么不在函数中处理新窗口的打开?通过在 onclick 函数上返回 false,您将停止对该事件的正常传播。
  • 对不起,我是 Javascript 新手,你能教我怎么做吗?

标签: javascript button onclick show-hide


【解决方案1】:

为什么 return false;showHide` 后面。

执行顺序为:

showHide 会先被调用,然后 return false; 被调用,它会返回 false ,所以 href 的处理将停止。 当onclick事件结束且不返回false时,该页面将跳转或为目标打开新页面。

Ps:你这里的onclick事件不是showHide('example');是:

 {
    showHide('example');
    return false;
}

【讨论】:

  • 嘿JsonSong!感谢您的帮助 !它在我刚刚删除 return false 时起作用; !谢谢!
【解决方案2】:

尝试将target="_blank" 添加到您的a 标签中,如下所示:

<a target="_blank" href="https://www.facebook.com/sharer/sharer.php?app_id=309437425817038&sdk=joey&u=http%3A%2F%2Fcloudsblack.com%2F180115f.html&display=popup&ref=plugin"
    id="example-show" class="showLink" onclick="showHide('example');return false;">     

【讨论】:

  • 谢谢乔尔。我试过了,但没有工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多