javascript - 将链接的参数存储在变量中,然后显示在它旁边的 span 标签中

标签 javascript jquery

尝试将数字存储在 team_id= 之后,然后将其显示在其下方的 span 标记中。我尝试使用 slice 来存储数字,但没有成功。

<div class="link"><a href="http://test/site/TR/ooo/General-ooo?team_id=3912&pg=team&event_id=1014">The Wolves</a>
<span></span>
</div>
<div class="link"><a href="http://test/site/TR/ooo/General-ooo?team_id=3912&pg=team&event_id=1060">The Tigers</a>
<span></span>
</div>
重点是从 URL 中提取数字,然后将其显示在相应的 span 标记中。

最佳答案

是这样的吗?

$("div.link a").each(function() {
  let $this = $(this),
      url = $this.attr("href").match(/team_id=(\d+)/);
      
  $this.next("span").text(url[1]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="link"><a href="http://test/site/TR/ooo/General-ooo?team_id=3912&pg=team&event_id=1014">The Wolves</a>
<span></span>
</div>
<div class="link"><a href="http://test/site/TR/ooo/General-ooo?team_id=3912&pg=team&event_id=1060">The Tigers</a>
<span></span>
</div>

解释:

  • $("div.link a").each(function() { - 选择 divlink 下的所有 anchor ;

  • url = $this.attr("href").match(/team_id=(\d+)/); - 获取带有 team_id 的 url 部分> 和以下号码;

  • $this.next("span").text(url[1]); - 设置当前 anchor 旁边的 span 的文本, 从 url 中获取的数字。

关于javascript - 将链接的参数存储在变量中,然后显示在它旁边的 span 标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51638143/

相关文章:

javascript - 在 JavaScript 中动态构建表单字段名称并分配值

javascript - Jquery:如何使用 onload 事件动态绑定(bind)文本框?

jquery - 自动调整 body 背景图像到浏览器窗口?

jquery - 当取消选中复选框时,找到所有具有匹配类的 div 并将其删除。查询

javascript - 如何用 Jest 测试异步等待 pg 连接?

javascript - Aloha 内联编辑器

javascript - 为什么 "[] + {} === {} + []"评估为 "true"?

javascript - 从 google chrome 扩展程序在后台运行音乐

jquery - Phantomjs 分页后 : always creating spaces

javascript - 使用 jquery 排除具有特定类的元素