简体   繁体   中英

Replacing html entities with a text using javascript

Please I want to replace html entities with a text like so: <img src='my_image.jpg'> so I ran this code:

var image = $("#my_div").html($("#my_div").html().replace(/<img scr='(.*?)'>/g, "{{$1}}"));

so when its outputted it show like this: {{my_image.jpg}} but when outputted this is what displays: [object Object] . Please I need help because I know am getting something wrong.

You can change an img element's attribute (src in this case) like this :

Markup:

<img id="eximg" src="source.jpg">

Script:

$('#eximg').attr('src','anothersource.jpg');

You can use a function to create the new value

<img id="myid" src="mypicture.jpg">

<script>
$('#myid').attr('src', function(i, origValue){
    return "{{" + origValue + "}}";
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM