JQuery的 - 如何添加一个HTML标签到一些HTML? [英] JQuery - How to add a single html tag to some html?
问题描述
我想在下面的文本(在LINK之前)插入一个< span> :
< li>< a href => LINK< / a>< / li>
因此,以上变成
<$ p $ < code>< li>< a href =>< span> LINK< / a>< / li>
以下是我的JQuery代码:
$('#mainnav li a')。prepend('< span>');
当我在firebug中查看代码后,我看到< span> < / span>
,我该如何获得开启span标签而不是结束标签?
$(li> a)。each(function(){
$(this) .contents()。wrapAll(< span>);
});
输入:
< UL>
< li>< a href =...>测试< / a>< / li>
< li>< a href =...>富< b>内容< / b>与< i>内部标签< / i>< / a>< / li>
< / ul>
输出:
< UL>
< li>< a href =...>< span>测试< / span>< / a>< / li>
< li>< a href =...>< span>富< b>内容< / b>与< i>内部标签< / i>< / span>< / a>< / li>
< / ul>
I want to insert just a single <span> before the text below (right before LINK):
<li><a href="">LINK</a></li>
So, the above becomes
<li><a href=""><span>LINK</a></li>
Here is my JQuery Code:
$('#mainnav li a').prepend('<span>');
When I view the code in firebug after, I see <span></span>
, how do I just get the opening span tag and not the closing tag?
Firstly, what you want is malformed HTML. jQuery is going to make it hard if not impossible to create that. I would suggest doing it in a single step rather than multiple. Try:
$("li > a").each(function() {
$(this).contents().wrapAll("<span>");
});
Input:
<ul>
<li><a href="...">testing</a></li>
<li><a href="...">rich <b>content</b> with <i>inner tags</i></a></li>
</ul>
Output:
<ul>
<li><a href="..."><span>testing</span></a></li>
<li><a href="..."><span>rich <b>content</b> with <i>inner tags</i></span></a></li>
</ul>
这篇关于JQuery的 - 如何添加一个HTML标签到一些HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!