开发者

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

开发者 https://www.devze.com 2023-03-17 02:11 出处:网络
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.

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

精彩评论

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