哪个是最好的方法。使用按钮打开链接
<button type="button" onclick="location='permalink.php'">Permalink</button>
<button type="button" href="index.php">Permalink</button>
哪个是最好的方法。使用按钮打开链接
<button type="button" onclick="location='permalink.php'">Permalink</button>
<button type="button" href="index.php">Permalink</button>
如果您使用 jquery,您可以像这样编写第二个按钮
<button type="button" id="SecondButton" data-href="index.php">Permalink</button>
然后添加一些javascript:
<script type="text/javascript">
$('#SecondButton').click(function(e) {
e.preventDefault(); e.stopPropagation();
window.location.href = $(e.currentTarget).data().href;
});
</script>
编辑(进一步完成的场景)
<button type="button" id="SecondButton" data-href="index.php" onclick="location='permalink.php'" href="index.php">Permalink</button>
我也会在其中插入额外的href标签和onclick;然后你可以测试不同的场景;不支持 javascript 或 jquery 的设备无法在移动设备等 CDN 上加载:
<button type="button" id="SecondButton" data-href="index.php?trackAnalytics=1" onclick="location='permalink.php?trackAnalytics=2'" href="index.php?trackAnalytics=3">Permalink</button>
如果您使用的是引导程序,则可以执行此操作...
<a class="btn btn-default" href="permalink.php">Permalink</a>
第一个应该可以工作,但不推荐使用内联脚本。您应该阅读有关如何使用addEventListener
符合标准的浏览器和attachEvent
旧 IE 附加事件的信息。
第二个不起作用,因为按钮不使用href
属性。
或者,如果按钮“样式”是您所追求的,而不是专门的<button>
标签,您可以这样做:
<form method="link" action="permalink.php">
<input type="submit" value="Permalink">
</form>
另一个想法是您可以将锚定样式设置为看起来像一个按钮,然后您可以使用 href 属性。