我有一个这样的清单
<ul class="example">
<li>
<span class="type"></span>
<a href="#">New document.txt</a>
</li>
<li>
<span class="type"></span>
<a href="#">Another document.zip</a>
</li>
</ul>
我怎样才能得到“。”之后的文字 并插入到“类型”?结果应该是这样的
<ul class="example">
<li>
<span class="type">txt</span>
<a href="#">New document.txt</a>
</li>
<li>
<span class="type">zip</span>
<a href="#">Another document.zip</a>
</li>
</ul>
迭代跨度并.
使用相邻同级中的 dot( )之后的文本更新文本内容。
// use text method with callback to iterate and update
// based on the returned value
$('.example .type').text(function() {
return $(this)
// get the a tag
.next()
// get text content
.text()
// split the text
.split('.')
// get the last element from array
.pop();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="example">
<li>
<span class="type"></span>
<a href="#">New document.txt</a>
</li>
<li>
<span class="type"></span>
<a href="#">Another document.zip</a>
</li>
</ul>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句