jquery each 嵌套 问题 $("span").each(function(){ var xx=$(this).html(); $(this).replaceWith(xx); }) <span>hi1<span>hi2hi2</span>hi1</span><span>hi3</span> 问题: 当有span嵌套时,上面运行不给力,去除不掉嵌套中的 hi2的 span标签 ? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 哦不对还是把外层的 span换成其它标签 简单点 $("span:has(span)") 试试试了不行啊 可以试试 $("apan > apan") <span>hi1<span>hi2hi2</span>hi1</span><span>hi3</span> 转成你最终生成是什么<span>???</span> 去掉所有 span 标签$('span').remove() 只去掉 span 标签 内容保留 定位他们的父级1. $('xxx').text();2. $('xxx').html().replace(/<\/?span[^>]*?>/ig,""); <div id="spnas"><span>hi1<span>hi2hi2</span>hi1</span><span>hi3</span> </div><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script> alert( $('#spnas').text() ) //简单方法 alert( $('#spnas').html().replace(/<\/?span[^>]*?>/ig,"") ) //为保留其它标签$('#spnas').html( $('#spnas').text() ) ); //重新设置HTML</script> 注: span 里面 有时 会有 class 的 <span id="id1" class="cs1">hi1<span class="cs2">hi2hi2</span>hi1</span><span>hi3</span> 谢了,有效!但这样做的话,需要id,实际应用需要获取当前鼠标划过的文本上级id,比较麻烦.没有span each 这样简单.请问jquery中有没有比较简单的 把所有 span 无论嵌套与否全部去掉呢!万分谢谢 如同网上找到的: function ShowDirection() { var tbl = []; var direct = (document.selection && document.selection.createRange) ? document.selection.createRange().parentElement() // IE : window.getSelection().focusNode.parentNode; //FF alert(direct.tagName) do { tbl.push(direct.tagName); } while((direct = direct.parentNode) && (direct !== document.documentElement)); alert(tbl.reverse().join('---->')) } <style> span{ color:red } </style> <div id="spnas"> 111 <span>hi1<span>hi2hi2</span>hi1</span><span>hi3</span> 222 </div> <button id="bnt1" >clear Span</botton><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script> $('#bnt1').click(function(){ $('*').each(function(){ if(this.tagName=='SPAN'){ $(this).before( $(this).text()); $(this).remove(); } }); }); </script> 还是正则吧,有问题再说.正则中 $('#spnas').html( $('#spnas').text() ) ); //重新设置HTML这个是错的, $('#spnas').text() ) 这个不用前面的正则就可以把所有的标签去掉了.谢谢结贴 js为jsp页面元素赋值。为taglib参数赋值。 关于Jquery中的 $.fn的作用 如何截获网页中嵌入的FLASH播放器的鼠标事件? 页面宽度问题 问一个简单的JS函数定义问题 如何用for 循环得出多个span的值?? 两种js特效冲突怎么解决? 可怜的IE11 修改submit的type属性 jQuery中两个对象执行相同的函数,怎么合并? 怎么把DIV变成一个圆 头痛的jquery 菜单,请高手们帮忙啊
还是把外层的 span换成其它标签 简单点
转成你最终生成是什么
<span>???</span>
$('span').remove()
1. $('xxx').text();
2. $('xxx').html().replace(/<\/?span[^>]*?>/ig,"");
<div id="spnas">
<span>hi1<span>hi2hi2</span>hi1</span><span>hi3</span>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
alert( $('#spnas').text() ) //简单方法
alert( $('#spnas').html().replace(/<\/?span[^>]*?>/ig,"") ) //为保留其它标签
$('#spnas').html( $('#spnas').text() ) ); //重新设置HTML
</script>
<span id="id1" class="cs1">hi1<span class="cs2">hi2hi2</span>hi1</span><span>hi3</span>
谢了,有效!
但
这样做的话,需要id,实际应用需要获取当前鼠标划过的文本上级id,比较麻烦.
没有span each 这样简单.
请问jquery中有没有比较简单的 把所有 span 无论嵌套与否全部去掉呢!万分谢谢
var tbl = [];
var direct = (document.selection && document.selection.createRange)
? document.selection.createRange().parentElement() // IE
: window.getSelection().focusNode.parentNode; //FF
alert(direct.tagName)
do
{
tbl.push(direct.tagName);
}
while((direct = direct.parentNode) && (direct !== document.documentElement));
alert(tbl.reverse().join('---->'))
}
span{ color:red }
</style>
<div id="spnas">
111 <span>hi1<span>hi2hi2</span>hi1</span><span>hi3</span> 222
</div>
<button id="bnt1" >clear Span</botton>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
$('#bnt1').click(function(){
$('*').each(function(){
if(this.tagName=='SPAN'){
$(this).before( $(this).text());
$(this).remove();
}
});
});
</script>
结贴