开发者

Replace '&' with an image using jQuery/CSS?

Is there a way I can change the basic text version of '&' and replace it with an ima开发者_开发知识库ge? I was thinking can we do it in jQuery? I don't want to use php for this.

Or is there a way to target it using css?

Thanks.


Yes, you could do it like this (asuming you want to replace inside #container element):

var origText = $("#container").text();
$("#container").text(origText.replace(/&/g, "__img_mark__"));
var intermediateHtml = $("#container").html();
$("#container").html(intermediateHtml.replace(/__img_mark__/g, '<img src="ampersand.gif" />'));

We're doing it in 2 steps because we don't want ampersands in the html code to be replaced

Hope this helps. Cheers


You could probably search for & or &amp; and use the String.replace method to replace it with your image.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜