Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用.replace将文本替换为HTML?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 使用.replace将文本替换为HTML?

Javascript 使用.replace将文本替换为HTML?,javascript,jquery,html,Javascript,Jquery,Html,我想使用jQuery将文本替换为图像。我一直在使用.replace函数将文本替换为更多文本。如何用html标记替换文本,如' $(this.text(text.replace(':trump:',image.outterHTML)); }); } 设定间隔(哇,1000); 以下是HTML: function wow() { $('.messageBody').each(function() { var text = $(this).text(); var imag

我想使用jQuery将文本替换为图像。我一直在使用.replace函数将文本替换为更多文本。如何用html标记替换文本,如' $(this.text(text.replace(':trump:',image.outterHTML)); }); } 设定间隔(哇,1000); 以下是HTML:

function wow() {
  $('.messageBody').each(function() {
      var text = $(this).text();
      var image = '<img class = "emote" src = "trump.png">'
      $(this).text(text.replace(':trump:', image.outterHTML));
  });
}
setInterval(wow, 1000);
:特朗普:

如果您使用
.html
而不是
.text
它将起作用。更改此行:

<span class="messageBody">:trump:</span>
  $(this).html(text.replace(':trump:', image));
为此:

  $(this).text(text.replace(':trump:', image.outterHTML));
注意:因为
image
是一个字符串,所以不需要
.outerHTML

如果
messageBody
中不仅仅包含文本(它包含HTML),那么您还需要更改此行:

<span class="messageBody">:trump:</span>
  $(this).html(text.replace(':trump:', image));
为此:

  var text = $(this).text();
因此,完整的代码是:

  var text = $(this).html();
函数wow(){
$('.messageBody')。每个(函数(){
var text=$(this.html();
var图像=“”;
$(this.html(text.replace(':trump:',image));
});
}
设定间隔(哇,1000);

如果您使用
.html
而不是
.text
它将起作用。更改此行:

<span class="messageBody">:trump:</span>
  $(this).html(text.replace(':trump:', image));
为此:

  $(this).text(text.replace(':trump:', image.outterHTML));
注意:因为
image
是一个字符串,所以不需要
.outerHTML

如果
messageBody
中不仅仅包含文本(它包含HTML),那么您还需要更改此行:

<span class="messageBody">:trump:</span>
  $(this).html(text.replace(':trump:', image));
为此:

  var text = $(this).text();
因此,完整的代码是:

  var text = $(this).html();
函数wow(){
$('.messageBody')。每个(函数(){
var text=$(this.html();
var图像=“”;
$(this.html(text.replace(':trump:',image));
});
}
设定间隔(哇,1000);
可以使用,它还将循环所有实例

function wow() {
  $('.messageBody').each(function() {
      var text = $(this).html();
      var image = '<img class="emote" src="trump.png">';
      $(this).html(text.replace(':trump:', image));
  });
}
setInterval(wow, 1000);
$('.messageBody').html(函数(\ux,现有html){
变量图像=“”
返回现有HTML.replace(':trump',image);
});
使用
text()

function wow() {
  $('.messageBody').each(function() {
      var text = $(this).html();
      var image = '<img class="emote" src="trump.png">';
      $(this).html(text.replace(':trump:', image));
  });
}
setInterval(wow, 1000);
$('.messageBody').html(函数(\ux,现有html){
变量图像=“”
返回现有HTML.replace(':trump',image);
});
使用
text()

$('.messageBody').html(function(_, existingHtml){
    var image = '<img class = "emote" src = "trump.png">'
    return existingHtml.replace(':trump', image);
});
函数wow(){
$('.messageBody')。每个(函数(){
var text=$(this.text();
变量图像=“”
$(this.html(text.replace(':trump:',image));
});
}
设定间隔(哇,1000);
将代码更改为:

$('.messageBody').html(function(_, existingHtml){
    var image = '<img class = "emote" src = "trump.png">'
    return existingHtml.replace(':trump', image);
});
函数wow(){
$('.messageBody')。每个(函数(){
var text=$(this.text();
变量图像=“”
$(this.html(text.replace(':trump:',image));
});
}
设定间隔(哇,1000);

谢谢!我不明白如何让代码在每次出现另一条消息时循环,谢谢!谢谢你!我不明白如何让代码在每次出现另一条消息时循环,谢谢!