jQuery技术:使用jQuery从文本变量中选择带有选择器的标签

我有一个字符串,其中包含文本和一些标签; 我想知道如何从变量中选择一个标签并循环它。 我尝试了以下但它不起作用:

 var text = `some string here with http:something.com more string and more links also`; $('a', text).each(function() { var string = $(this).html(); $(this).html(string.substring(0, length-1)+(string.length > length ? end : '')); }); 

    你需要将文本包装在div(或其他元素)中然后find()它:

     var text = 'some string here with http:something.com more string and more links also'; text = $('
    ' + text + '
    '); text.find('a').each(function() { var length = 10; var end = '...'; var string = $(this).html(); $(this).html(string.substring(0, length) + (string.length > length ? end : '')); }); var text = text.html(); // Put it into a textarea $('#myTextarea').val(text);

    更换

     $('a', text).each(function() { 

     $(text, 'a').each(function() { 

    并看看它是否有效。

      以上就是jQuery教程分享使用jQuery从文本变量中选择带有选择器的标签相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/jquerytutorial/524592.html

      (0)
      上一篇 2020年12月13日
      下一篇 2020年12月13日

      精彩推荐