方法如下:

  • 1. str.replace(/oldString/g,newString)
  • 2. str.replace(new RegExp(oldString,"gm"),newString)
  • 3. String 对象原型方法 replaceAll

示例代码:

  // 字符替换方法实现
        String.prototype.replaceAll = function(s1,s2){
            return this.replace(new RegExp(s1,"gm"),s2);
        }
 /**
     * 设置事件选项
     *
     * @param template
     */
    EventTemplate.prototype.setEventTemplates = function () {
        var that = this;
        that.eventListDiv.html("");
        var templates = that.getEventTemplates();
        for (var key in templates) {
            var template = templates[key];
            var key = template["KEY"];
            var name = template["NAME"];
            if(undefined == name || "" == name.trim()){
                continue;
            }
            var icon = template["ICON"];
            var templateHtml = '<div class="item">\n' +
                '            <input type="checkbox" name="sj-type"  >\n' +
                '            <label for="EVENT-#{eventKey}">#{name}</label>\n' +
                '            <img src="upload/event/' + icon + '">\n' +
                '        </div>';
            templateHtml = templateHtml.replaceAll("#{eventKey}",key);
            templateHtml = templateHtml.replaceAll("#{name}",name);
            that.eventListDiv.append(templateHtml);
            // 绑定点击事件
            $('#eventListDiv input:checkbox[>(){
                window.Event.recheckEvents();
            });
        }
    }

注意:替换字符表达式不能是${xxxxx}这种格式。

相关文章:

  • 2021-11-03
  • 2022-12-23
  • 2021-08-16
  • 2021-08-04
  • 2021-06-13
  • 2021-07-22
  • 2022-02-22
  • 2021-10-02
猜你喜欢
  • 2021-12-11
  • 2021-08-05
  • 2021-05-22
  • 2022-03-01
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
相关资源
相似解决方案