Wrap Each * on a Page Using jQuery

后端 未结 5 1338
挽巷
挽巷 2021-01-06 06:55

I need to wrap each asterisks on a page with . The few things I\'ve tried don\'t work. I think what this boils down to is

5条回答
  •  难免孤独
    2021-01-06 07:41

    This will work and not replace * in tags it shouldn't.

    http://jsfiddle.net/DR6Ca/2/

    var text = $("body").find(":contains(*)").contents().filter(function() {
        //Don't include css or script tags, all other text nodes are fine.
      return this.nodeType == 3
          && ($(this).parent().get(0).tagName.toUpperCase() !== "SCRIPT") 
          && ($(this).parent().get(0).tagName.toUpperCase() !== "STYLE");
    }).replaceWith(function() {
       return this.textContent.replace(/\*/g, "*");
    

    You can test the others code in this jsfiddle to see if they keep the "hi" blue or not. If it doesn't stay blue they have a bug.

提交回复
热议问题