html - 是否可以右键单击表单元素来打开新页面?

标签 html css

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

enter image description here

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

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

最佳答案

正如 Jon 在对您的问题的评论中建议的那样,您可以设置 <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 */
}

我用了css3generatorgradient editor

关于html - 是否可以右键单击表单元素来打开新页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16619436/

相关文章:

javascript - 如何创建没有名称的 "Empty"<Label> 标记

javascript - AngularJS - 按范围将对象集合绑定(bind)到 HTML 表

javascript - 如何快速找到绝对定位的子元素的相对定位的 HTML 父元素?

html - 基于对象宽度的 TranslateY

html - 如何使用 selenium IDE 查找/检查 div 对象是否有子对象?

javascript - html 内的复选框检查

HTML5 <audio> 直播流的糟糕选择?

javascript - 一次切换两个 div 的可见性

html - 垂直对齐 flexbox 元素并保持相同的宽度

html - 来自父级的子主题 style.css 不传输