如何用Imgae替换[Img] [/ Img]标签 [英] How Do I Replace A [Img] [/Img] Tag With An Imgae

查看:99
本文介绍了如何用Imgae替换[Img] [/ Img]标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人使用 [img] [/ img] 标记对Blogger发表评论,则会发布此HTML代码。假设代码类似

 <   div     id   =  mm  >  [img] https://www.gravatar.com/avatar/4ce47f47105a3bd1ca4700f02b5deff1?s=32&d=identicon&r=PG [ / img] <   / div  >  

我们的javascript应找到[img]标签并将其替换为图像。所以javascript可能是这样的:

  var  s; 
s = document .getElementById( mm )。innerHTML
s = s.replace( [img] ');
s = s.replace( [/ img] ');
var b;
b = < img src = + s + >< / img>
document .getElementById ( mm)。innerHTML = b;



当div id =mm仅包含[img] [/ img]标签时,它可以工作。但是当它们是其他文本或代码时,它就无法显示e图像。请帮我。在此先感谢

解决方案

您要将 [img] [/ img] 结构的内容放入

 <   img     src   = 在JS执行后,您捕获的内容将在此处输入 >  <   / img  >  





如果 [img] [/ img] 的内容没有生成有效的src属性,显然它不会显示预期的图像。



干杯。

C


我修改了demo的代码,下面的代码按预期工作:

(用于验证我已经为url设置了警报,你可以删除它)



< div id =mm>一些废话文本[img] https://www.gravatar.com/avatar/ 4ce47f47105a3bd1ca4700f02b5deff1?s = 32& d = identicon& r = PG [/ img]一些废话文本
< / div>

< script>
var s;
s = document.getElementById(mm)。innerHTML
var afterImg = s.split([img])[1];
var url = afterImg.split([/ img])[0];
alert(url);
var b =< img src =+ url +>< / img>
document.getElementById(mm)。innerHTML = b;
< / script>





如果有任何进一步的问题,请与我们联系。感谢。


When someone comment on Blogger with [img][/img] tag then this HTML code is published. Assume the code like that

<div id="mm">[img]https://www.gravatar.com/avatar/4ce47f47105a3bd1ca4700f02b5deff1?s=32&d=identicon&r=PG[/img]</div>

Our javascript should find the [img] tag and replace that with an image. So javascript may be like this:

var s;
s = document.getElementById("mm").innerHTML
s = s.replace("[img]","'");  
s = s.replace("[/img]","'"); 
var b;
b = "<img src="+s+"></img>"
document.getElementById("mm").innerHTML = b;


it works when the div id="mm" contains only [img] [/img] tag. but when their is other text or code then it fails to thdisplay e image. Please help me. Thanks in advance

解决方案

You are placing the content of the [img][/img] structure into the

<img src="Your captured content enters here after the JS execution"></img>



If the content of the [img][/img] does not make a valid src property, obviously it will not show the intended image.

Cheers.
C


I have modified the code for demo and below code is working as expected:
(for verification I have put alert for url, you can remove that)

<div id="mm">Some Crap Text[img]https://www.gravatar.com/avatar/4ce47f47105a3bd1ca4700f02b5deff1?s=32&d=identicon&r=PG[/img]Some Crap Text
</div>

<script>
    var s;
    s = document.getElementById("mm").innerHTML
    var afterImg = s.split("[img]")[1];
    var url = afterImg.split("[/img]")[0];
    alert(url);
    var b = "<img src=" + url + "></img>"
    document.getElementById("mm").innerHTML = b;
</script>



Please let me know if any further issue. Thanks.


这篇关于如何用Imgae替换[Img] [/ Img]标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆