href = javascript:{}做什么用?

问题描述 投票:2回答:2

在下面的代码中使用javascript:{}的原因是什么。它与href="javascript:"href="javascript:void(0)"相似吗?

<a href="javascript:{}">This is an example</a>
javascript html href
2个回答
5
投票

让超链接看起来像一个链接,但没有链接任何东西。

<a href="javascript:{}">This is an example</a>

如果你删除href属性,那a标签将是普通文本。


4
投票

它使按钮像链接一样,但允许您执行自定义JS代码而不是链接到网页。

例如:

<a href="javascript:{ document.write('hi u just pressed me');}"> Press me pls </a>

表现得像

<div onclick="doOnClick()"> Press me pls </a>
<script>
    function doOnClick(){ document.write('hi u just pressed me'); }
</script>

但浏览器将前者视为链接。

© www.soinside.com 2019 - 2024. All rights reserved.