我创建了一个按钮,我希望其行为类似于链接,但无法右键单击:
如何才能右键单击该元素来创建链接而不是“返回”等?我现在使用的代码是:
<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 */
}
关于html - 是否可以右键单击表单元素来打开新页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16619436/