哪种方法最好。使用按钮打开链接
<button type="button" onclick="location='permalink.php'">Permalink</button>
<button type="button" href="index.php">Permalink</button>
答案 0 :(得分:4)
如果您使用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>
答案 1 :(得分:1)
第一个应该可以工作,但不建议使用内联脚本。您应该阅读有关如何使用addEventListener
为符合标准的浏览器添加事件以及attachEvent
用于旧IE的事件。
自buttons don't use the href
attribute以来第二个不起作用。
答案 2 :(得分:1)
如果你使用bootstrap,你可以这样做......
<a class="btn btn-default" href="permalink.php">Permalink</a>
答案 3 :(得分:-1)
或者,你可以做这样的事情,如果按钮'风格'是你所追求的,而不是特别是<button>
标签:
<form method="link" action="permalink.php">
<input type="submit" value="Permalink">
</form>
另一个想法是,您可以将锚设置为看起来像一个按钮,然后您可以使用href属性。