开发者

Transform string into image with jQuery

Another jQuery question for you guys.

Say I have a url of an image in my web page surrounded by square brac开发者_运维百科kets.

[http://www.example.com/picture.jpg]

How could I, with jQuery transform that string like so...

[http://www.example.com/picture.jpg]

into...

<img src="http://www.example.com/picture.jpg" />

?


I'd do something like this

$("some-selector").each(function(){
    $(this).html($(this).html().replace(/\[(http:.*?)\]/gi, function(str, p1){
        return "<img src='"+p1+"' />";
    }));
});

"some-selector" should try to pinpoint where these string occur. If there is nothing like it... just put "body" and see what happens :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜