Jquery 如何删除html字符串中的元素 基础知识 名单 名单 基础知识 名单 基础知识

Jquery 如何删除html字符串中的元素 基础知识 名单 名单 基础知识 名单 基础知识,jquery,Jquery,这是myString变量,现在我想从这个字符串中删除span元素?因此,myString将是: <div> <span>ABC</span> <ul> <li>list</li> <li>list</li> <span>ABC</span>

这是myString变量,现在我想从这个字符串中删除span元素?因此,myString将是:

   <div>
       <span>ABC</span>
        <ul>
             <li>list</li>
             <li>list</li>
             <span>ABC</span>
             <li>list</li>
        <ul>
        <span>ABC</span>
  </div>

  • 名单
  • 名单
  • 名单
谢谢
(remove()仅适用于顶级元素)

您可以使用substr函数执行搜索并手动删除它

您可以使用substr函数执行搜索并手动删除它

尝试:

   <div>
        <ul>
             <li>list</li>
             <li>list</li>
             <li>list</li>
        <ul>
  </div>
var str='ABC
  • 1
  • 2
ABC'; 不带_span=$(str)的var.find('span').remove().end();
end()获取更新的字符串(从技术上讲,返回堆栈中的最后一个选择,即在查找之前)

演示

试试:

   <div>
        <ul>
             <li>list</li>
             <li>list</li>
             <li>list</li>
        <ul>
  </div>
var str='ABC
  • 1
  • 2
ABC'; 不带_span=$(str)的var.find('span').remove().end();
end()获取更新的字符串(从技术上讲,返回堆栈中的最后一个选择,即在查找之前)

演示

它对我有用

var str = '<div><span>ABC</span><ul><li>1</li><li>2</li></ul><span>ABC</span></div>';
var without_span = $(str).find('span').remove().end(); 
它对我有用

var str = '<div><span>ABC</span><ul><li>1</li><li>2</li></ul><span>ABC</span></div>';
var without_span = $(str).find('span').remove().end(); 
你可以试试:

$('div').find('span').remove();
我建议您在UI元素上分配一些类。

您可以尝试:

$('div').find('span').remove();

我建议您在UI元素中分配一些类。

有点硬编码,但可以工作

$('ul').find(span:first).remove();
var str='ABC
    • ABC'; stringToReplace='ABC'/*在此处输入要替换的字符串*/ var指数=str.indexOf(stringToReplace); while(索引!=-1){ str=str.replace(stringToReplace“”); 索引=str.indexOf(stringToReplace); } 警报(str);​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

有点硬编码,但可以工作

$('ul').find(span:first).remove();
var str='ABC
    • ABC'; stringToReplace='ABC'/*在此处输入要替换的字符串*/ var指数=str.indexOf(stringToReplace); while(索引!=-1){ str=str.replace(stringToReplace“”); 索引=str.indexOf(stringToReplace); } 警报(str);​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

remove也可以在其他地方工作,选中remove也可以在其他地方工作,选中我想删除所有span元素,不管它们在哪里$(document).find(span).remove()$('span').each(function(){$(this.remove();}我想删除所有span元素,不管它们在哪里$(document.find(span).remove();$('span')。each(function(){$(this.remove();}@Snoob
end
获取更新的字符串(从技术上讲,返回堆栈中的最后一个选择,即
find
之前).这里是同一解决方案的另一个示例:.+1很好,正如我所想:)但我没有考虑
end()mean@Snoob
end
获取更新的字符串(从技术上讲,返回堆栈中的最后一个选择,即在
find
之前)。下面是相同解决方案的另一个示例:.+1很好,正如我所想:)但是我没有想到它是什么意思