迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

JavaScript href 表达式

作者:迹忆客 最近更新:2024/03/20 浏览次数:

本文将帮助你了解 href 表达式及其在 JavaScript 中的工作机制。


JavaScript href 表达式

HTML 元素(或锚元素)上的 href 属性生成指向网页、文件、电子邮件地址、页面位置或 URL 可以表示的任何其他内容的超链接。

<p>You can reach to this link:</p>
<ul>
  <li><a href="https://jsfiddle.net/">JSFiddle</a></li>
</ul>

在此处运行代码。

输出:

要保持在同一页面上,你还可以在 href 属性中提供名称或空 URL。

<a href="javascript:;"></a>

上面的代码通常用于创建链接,而无需在 href 属性中指定真正的 URL。


在 JavaScript 中使用带有哈希的 href 表达式

哈希 # 指定一个 HTML 元素 id,其中窗口应在超链接中拖动。

尽管 href="#" 没有提供 id 名称,但它确实指定了一个位置 - 页面顶部。当你单击带有 href="#" 的锚点时,滚动位置将移动到顶部。

<h1>Scroll to the bottom of the page and click the link</h1>
<a href="#">Scroll to Top</a>

你可以在此处查看演示。


在 JavaScript 中将 href 表达式与函数一起使用

基本上,不是使用链接来移动页面(或锚点),而是使用此方法将启动一个或多个 JavaScript 函数。

<html>
  <body>
    <script>
function doSomething() {
  alert("hello Delft")
}
</script>
<a href="javascript:doSomething();">click me</a>
  </body>
</html>

在此处运行代码。

当你单击该链接时,它会触发警报,调用 JavaScript 函数,你将看到一个弹出窗口,上面写着 hello Delft

输出:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便