Finding text node

前端 未结 8 1259
半阙折子戏
半阙折子戏 2021-01-03 08:14

Is there a clever jQuery selector for selecting a text node like this:

one two three
相关标签:
8条回答
  • 2021-01-03 08:49
    //finds most inner element that has 'three' in it
    var el = $(':contains(three):last');
    //actual replacement
    el.html(el.html().replace('three', '<strong>three</strong>'));
    
    0 讨论(0)
  • 2021-01-03 08:54

    If it's not about the last word, but about the div's pure content try this:

    var chld=$('div').children().clone();
    $(div).children().remove();
    //now do what You want with the text, eg.
    $(div).html('<strong>'+$(div).text()+'</strong>'); //or something like that :)
    $(div).prepend(chld); //This is a bottleneck, cause You have to know that 
              //all the text was after all the markup, but it refers to Your example.
    
    0 讨论(0)
提交回复
热议问题