Onclick或href最适合在按钮中打开链接 [英] Onclick or href which is best for opening an link in button

查看:71
本文介绍了Onclick或href最适合在按钮中打开链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用按钮打开链接

Which is the best method.To open an link using button

<button type="button" onclick="location='permalink.php'">Permalink</button>

<button type="button" href="index.php">Permalink</button>


推荐答案

如果您使用jquery,您可以编写第二个按钮

If you use jquery you can write the second button like this

<button type="button" id="SecondButton" data-href="index.php">Permalink</button>

然后添加一些javascript:

and then add some javascript:

<script type="text/javascript">
    $('#SecondButton').click(function(e) {
        e.preventDefault(); e.stopPropagation();
        window.location.href = $(e.currentTarget).data().href;
    });
</script>

编辑(进一步完成的方案)

Edit (Further completed scenarios)

<button type="button" id="SecondButton" data-href="index.php" onclick="location='permalink.php'" href="index.php">Permalink</button>

我也会在里面插入额外的href标记和onclick;然后你可以测试不同的场景;不支持javascript或jquery的设备无法在移动设备等CDN上加载:

I would insert both additional href tag and onclick inside it too; you could then test for different scenarios; devices that do not support javascript or jquery fails to load on CDN like mobile etc:

<button type="button" id="SecondButton" data-href="index.php?trackAnalytics=1" onclick="location='permalink.php?trackAnalytics=2'" href="index.php?trackAnalytics=3">Permalink</button>

这篇关于Onclick或href最适合在按钮中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆