gpt4 book ai didi

javascript - 使用 Javascript Regex 在鼠标悬停期间获取 Href 的一部分

转载 作者:行者123 更新时间:2023-12-02 16:49:17 24 4
gpt4 key购买 nike

我有下面的 HTML 代码

<div class="first-div">
<ul>
<li><a href="http://localhost/wordpress/2014/04/18/post-format-audio-2/#php">PHP</a></li>
<li><a href="http://localhost/wordpress/2014/04/18/post-format-audio-2/#css">CSS</a></li>
<li><a href="http://localhost/wordpress/2014/04/18/post-format-audio-2/#html">HTML</a></li>
</ul>
</div>

<div class="second-div"></div>

我想要当鼠标悬停在 PHP、CSS 和 HTML 链接上时:

  1. 创建 Javascript REGEX 以仅从第一个 div 中的 href 获取不带 # 符号的 (php,css,html)。
  2. 然后将 (php,css,html) 作为 ID 添加到第二个 div。

最佳答案

您不需要正则表达式,# 后面的部分是哈希,可以直接使用。

$('a').on('mouseover', function(e) {
e.preventDefault();

$('.second-div').prop('id', this.hash.substr(1));
});

不知道为什么要动态设置 ID,但无论什么都让你兴奋!

enter image description here

关于javascript - 使用 Javascript Regex 在鼠标悬停期间获取 Href 的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26821701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com