gpt4 book ai didi

jQuery 剪切链接

转载 作者:行者123 更新时间:2023-12-01 06:15:05 24 4
gpt4 key购买 nike

这是html:

<a href="http://site.com/any/different/folders/picture_name.jpg">Go and win</a>
<a href="http://site.com/not/similar/links/some_other_name.png">Go and win</a>

如何剪切a href中除picture_name.jpg之外的所有数据?链接的长度可以是任意的,我们必须只取从最后一个 / 到最后一个 "

的值

如果当前链接的 alttitle 相等,有人知道最短的比较方法吗?

谢谢。

最佳答案

如果您想实际将 href 分别更改为 picture_name.jpgsome_other_name.png,您可以将函数传递给 .attr() 并使用 .lastIndexOf()/.substring() ,如下所示:

$("a").attr('href', function(i, href) {
return href.substring(href.lastIndexOf('/') + 1);
});

You can view/play with a quick demo here

<小时/>

或者,如果您使用.each(),您可以获取/使用如下所示的值:

$("a").each(function() {
var new_href = this.href.substring(this.href.lastIndexOf('/') + 1);
//use it, e.g.: alert(new_href);
});​

You can see a demo of that here

关于jQuery 剪切链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3041370/

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