开发者

Transform string into image with jQuery

开发者 https://www.devze.com 2022-12-08 23:06 出处:网络
Another jQuery question for you guys. Say I have a url of an image in my web page surrounded by square brac开发者_运维百科kets.

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

精彩评论

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