例如我有简单的代码:
<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,如何剪切它们?
非常感谢您的帮助。
Cheers.
use
最后一个索引 https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String/lastIndexOf and 子串 https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/String/Substring
给锚标签一个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 ) )
});
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)