substring selector with jquery?

后端 未结 5 1094
南笙
南笙 2021-01-23 01:02

Is it possible to select only part of a string using jquery? For example I have a text

Metuentes igitur idem latrones Lycaoniam magna parte campestrem&l

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-23 01:21

    If you know the element in which to search:

    var string = "Lycaoniam";
    var e = $("elementSelector"); // put here your selector
    e.html(e.html().replace(new RegExp(search, "gi"), ""+search+""));
    

    for more elements, simply loop through them and do the replacing. To make a toggable styling, I'd use a custom class:

     e.html(e.html().replace(new RegExp(search, "gi"), ""+search+""));
    

    and remove it with:

    $(".highlight").contents().unwrap();
    

    .contents().unwrap() does the trick of stripping not only the class (which must be styled accordingly) but also the tag.

提交回复
热议问题