<scripttype="text/javascript"> // 替换代码为表情 window.onload=function(){ var div = document.getElementById('biaoqing'); var html = div.innerHTML; div.innerHTML = html.replace(/(\[emt])/g,"<img src="+"/img/").replace(/(\[)/g,"").replace(/(\/emt])/g,".gif />"); } </script>
如果渲染数据用的是接口怎么办呢?可以在拿到数据的时候遍历修改
1 2 3 4 5 6 7
let list = [ {content:"你好啊[emt]1[/emt]"}, {content:"你也好啊[emt]44[/emt]"} ] for(let i = 0; i < list.length; i++){ list[i].content = list[i].content.replace(/(\[emt])/g,"<img src="+"/img/").replace(/(\[)/g,"").replace(/(\/emt])/g,".gif />") }