尝试将数字存储在 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>
最佳答案
是这样的吗?
$("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() {
- 选择div
类link 下的所有 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/