Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在div中查找多个html标记_Jquery_Html - Fatal编程技术网

Jquery 在div中查找多个html标记

Jquery 在div中查找多个html标记,jquery,html,Jquery,Html,我想替换由em和u标记包装的HTML文本 例如: <div id='test'> This is the test and <em> <u> texts here will need to be replaced </u></em> to a link. </div> 我的代码有两个问题。 我似乎无法“找到”标签下的文本。如果我改为find('u')它会工作 2.我想为我的结果添加7个a标记,因此此处的“文本”将“需要”被

我想替换由
em
u
标记包装的HTML文本

例如:

<div id='test'>
This is the test and <em> <u> texts here will need to be replaced </u></em> to a link.
</div>
我的代码有两个问题。 我似乎无法“找到”标签下的文本。如果我改为
find('u')
它会工作

2.我想为我的结果添加7个a标记,因此
此处的“文本”将“需要”被“替换”
将可链接。我的代码仅为7个单词创建1个链接

有人能帮我吗?

试试看

jQuery(function($){
    var texts = $('#test');
    texts.find('em > u').html(function(idx, html){
        return html.replace(/(\w+)/g, '<a href="#" class="link" onclick="return false;">$1</a>')
    })
})
jQuery(函数($){
变量文本=$(“#测试”);
text.find('em>u').html(函数(idx,html){
返回html.replace(/(\w+)/g'')
})
})
演示:

试试看

jQuery(function($){
    var texts = $('#test');
    texts.find('em > u').html(function(idx, html){
        return html.replace(/(\w+)/g, '<a href="#" class="link" onclick="return false;">$1</a>')
    })
})
jQuery(函数($){
变量文本=$(“#测试”);
text.find('em>u').html(函数(idx,html){
返回html.replace(/(\w+)/g'')
})
})
演示:

我建议:

$('#test u').html(function(){
    var words = $(this).text().split(/\s+/);
    for (var i = 0, len = words.length; i<len; i++){
        if (words[i].length) {
            words[i] = '<a href="#">' + words[i] + '</a>';
        }
    }
    return words.join(' ');
});

要同时删除
u
em
元素(无论每个元素中嵌套了哪一个),下面的方法很有效,但感觉很凌乱,坦率地说,使用有序/正确结构的HTML将大大受益:

$('#test u > em, #test em > u').html(function () {
    var words = $(this).text().split(/\s+/);
    for (var i = 0, len = words.length; i < len; i++) {
        if (words[i].length) {
            words[i] = '<a href="#">' + words[i] + '</a>';
        }
    }
    return words.join(' ');
}).find('a').unwrap().unwrap();
$('#test u>em,#test em>u').html(函数(){
var words=$(this.text().split(/\s+/);
for(var i=0,len=words.length;i

参考资料:

    • 我建议:

      $('#test u').html(function(){
          var words = $(this).text().split(/\s+/);
          for (var i = 0, len = words.length; i<len; i++){
              if (words[i].length) {
                  words[i] = '<a href="#">' + words[i] + '</a>';
              }
          }
          return words.join(' ');
      });
      

      要同时删除
      u
      em
      元素(无论每个元素中嵌套了哪一个),下面的方法很有效,但感觉很凌乱,坦率地说,使用有序/正确结构的HTML将大大受益:

      $('#test u > em, #test em > u').html(function () {
          var words = $(this).text().split(/\s+/);
          for (var i = 0, len = words.length; i < len; i++) {
              if (words[i].length) {
                  words[i] = '<a href="#">' + words[i] + '</a>';
              }
          }
          return words.join(' ');
      }).find('a').unwrap().unwrap();
      
      $('#test u>em,#test em>u').html(函数(){
      var words=$(this.text().split(/\s+/);
      for(var i=0,len=words.length;i

      参考资料:

      var text=$(“#测试”);
      text.find('em u')。每个(函数(){
      var words=$(this.html().split(“”);
      var输出=“”;
      for(var i=0;i

      var text=$(“#测试”);
      text.find('em u')。每个(函数(){
      var words=$(this.html().split(“”);
      var输出=“”;
      for(var i=0;i

      使用正则表达式

      $('#test em u').each(function(){
          var $this = $(this); 
          var html = $this.html().replace(/\s?(\w+)\s?/g, "<a href='#'>$1</a>");
          $this.html(html);
      });
      
      $('#test em u')。每个(函数(){
      var$this=$(this);
      var html=$this.html().replace(/\s?(\w+)\s?/g,”);
      $this.html(html);
      });
      

      使用正则表达式

      $('#test em u').each(function(){
          var $this = $(this); 
          var html = $this.html().replace(/\s?(\w+)\s?/g, "<a href='#'>$1</a>");
          $this.html(html);
      });
      
      $('#test em u')。每个(函数(){
      var$this=$(this);
      var html=$this.html().replace(/\s?(\w+)\s?/g,”);
      $this.html(html);
      });
      

      他们应该链接到什么URL?@DavidThomas只是一个不真实的链接。虽然我对这个练习的目的仍然很好奇,但这很公平。他们应该链接到什么URL?@DavidThomas只是一个不真实的链接。很公平,尽管我对这个练习的目的仍然很好奇。谢谢,我有一个后续问题。如果html标记包含和混合了。我如何替换它们+1这取决于您想做什么,您是否只想删除
      u
      ?或者
      em
      u
      ?我想同时删除em和uThanks,我有一个后续问题。如果html标记包含和混合了。我如何替换它们+1这取决于您想做什么,您是否只想删除
      u
      ?或者同时使用
      em
      u
      ?我想同时删除em和u