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 &
and use the String.replace
method to replace it with your image.
精彩评论