【问题标题】:href onclick javascript right clickhref onclick javascript右键单击
【发布时间】:2014-04-08 03:07:49
【问题描述】:

我在 JavaScript 中有一个类似的函数

function continents(post_id,continent,countries){
   location.href="/serv/continent.php?param="+post_id+"&"+continent+"/"+countries;
}

然后我的 php 页面中有一个锚标记

<a href="javascript: void(0)" onclick="continents('<?php echo $row['sno']; ?>','<?php echo $url_con_name; ?>','<?php echo $url_contries; ?>')" >
   <div>some date here </div>
</a>

现在链接工作正常,但是当我右键单击该链接时,我没有找到 在新标签中打开链接在新窗口中打开链接。。 p>

如何在不删除该功能的情况下解决此问题。

【问题讨论】:

  • 删除href="javascript: void(0)" 和你在渲染的HTML 中得到了什么?
  • &lt;a href="/serv/continent.php?param=&lt;?php echo $row['sno'], '&amp;', $url_con_name, '/', $url_contries ?&gt;"&gt;怎么样
  • @Musa 谢谢它的工作,我之前尝试过和你的类似,但出了点问题,现在好了。

标签: javascript jquery html url


【解决方案1】:

a 标记中没有 href 属性,因此没有可打开的链接

解决方法是在您的 href 属性中放置一个正确的 URL。

由于您使用的是 jQuery,因此您可能不应该使用 onclick 属性。

做这样的事情:

<a href="http://example.com/proper/link" id="myLink">

然后通过jQuery附加JS逻辑:

$('#myLink').click(function(e){
    e.preventDefault() 
        // this will prevent the link from loading the 
        // href value upon click, but still leave it 
        // accessible to right-click contextual menu.
    the rest of your JS logic here...
})

【讨论】:

    【解决方案2】:

    添加href="#",点击事件有return false;

    试试

    <a href="#" onclick="continents('<?php echo $row['sno']; ?>','<?php echo $url_con_name; ?>','<?php echo $url_contries; ?>'); return false;" >
    <div>some date here </div></a>
    

    【讨论】:

    • 仍然没有适当的 href 属性,所以您不会在上下文菜单中获得选项。
    • @DA。我试过jsfiddle.net/tamilcselvan/ykeLF,它显示了上下文菜单。
    • 对,但它只是当前的 URL + #,所以可能不是 OP 想要的(因为没有理由在新标签页中打开同一页面)
    猜你喜欢
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 2011-01-25
    相关资源
    最近更新 更多