1

我创建了一个按钮,我希望它的行为类似于链接,但无法右键单击:

在此处输入图像描述

如何使右键单击元素以创建链接而不是“返回”等成为可能?我现在使用的代码是:

 <form action="/ai" method="get">
    <input type="submit" class="btn primary"  name="psearchsub" value="Post your ad for free"/>
</form>
4

3 回答 3

1

正如乔恩在对您问题的评论中所建议的那样,您可以<a>直接设置样式。如果您想要右键单击事件,请挂钩oncontextmenu并阻止默认操作:

<a href="http://leftclicklocation.com" id="ad">Post your ad for free</a>
<script>
  document.getElementById("ad").addEventListener("contextmenu",function(e) {
    e.preventDefault(); // to supress the default context menu
    window.location = "http://rightclicklocation.com"; // whatever you want
  });
</script>

这是风格

  #ad {
    display: inline-block;
    border: 1px solid black;
    width: 200px;
    height: 25px;
    text-decoration: none;
    text-align: center;
    color: white; font-weight: bold;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    background: #6db3f2; /* Old browsers */
background: -moz-linear-gradient(top,  #6db3f2 0%, #54a3ee 50%, #3690f0 51%, #1e69de 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6db3f2), color-stop(50%,#54a3ee), color-stop(51%,#3690f0), color-stop(100%,#1e69de)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  #6db3f2 0%,#54a3ee 50%,#3690f0 51%,#1e69de 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  #6db3f2 0%,#54a3ee 50%,#3690f0 51%,#1e69de 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  #6db3f2 0%,#54a3ee 50%,#3690f0 51%,#1e69de 100%); /* IE10+ */
background: linear-gradient(to bottom,  #6db3f2 0%,#54a3ee 50%,#3690f0 51%,#1e69de 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6db3f2', endColorstr='#1e69de',GradientType=0 ); /* IE6-9 */
}

我使用了 css3generator渐变编辑器

于 2013-05-18T00:18:38.110 回答
1

在表单元素上放置一个事件监听器。

$('form').on('click', function(e){
  //detects right click
  if ( e.originalEvent.button = 2 ) {
    window.location = 'http://newpage.com'
  }
});

如下所述,您可能想要覆盖上下文菜单(不知道您可以这样做)

于 2013-05-18T00:19:25.323 回答
1

编辑:对不起,我想我错误地解释了你的帖子。您希望能够右键单击按钮,还是不希望用户能够右键单击按钮?

原文: 这里只是一个想法-尝试使用oncontextmenu="return false;"-如果我没记错的话,应该可以防止右键单击。(当然,如果你愿意使用一点 JavaScript)

于 2013-05-18T00:13:39.790 回答