使用jQuery从'a'元素的'href'属性中删除文件名的最佳方法是什么? [英] What is the best way to cut the file name from 'href' attribute of an 'a' element using jQuery?

查看:62
本文介绍了使用jQuery从'a'元素的'href'属性中删除文件名的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有简单的代码:

For example I've got the simple code:

<ul class="list">
  <li><a href="http://www.aaa.com/bbb/ccc/file01.pdf">Download file 01</a></li>
  <li><a href="http://www.bbb.com/ccc/ddd/file02.pdf">Download file 02</a></li>
</ul>

我希望只在变量中存储文件名:file01.pdf和file02.pdf,如何我可以剪掉它们吗?

and I wish to store in variables only the file names: file01.pdf and file02.pdf, how can I cut them?

非常感谢您的帮助。

干杯。

推荐答案

使用

lastIndexof 子串

给锚标记一个id

var elem = document.getElementById ( "anch1" );

elem.href.substring (elem.href.lastIndexOf ( '/' ) + 1 );

使用jQuery

$(function() {
    $("ul.list li a").each ( function() {
        alert ( $(this).attr ( "href" ).substring ($(this).attr ( "href" ).lastIndexOf ( '/' ) + 1 ) )
    });
});

这篇关于使用jQuery从'a'元素的'href'属性中删除文件名的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆