简体   繁体   中英

str.replace() in a object

For my project i am pulling back data from a JSON file into a array, i am then looping through the data for each matching parameter that the user has entered. I want to replace a part of the string with an image where each object contains a certain letter. Example : {6} {w} {w} I want to replace each letter with a image that the letter represents? I tried $("#array").str.replace("{w}", "../images/wImage.png"); Thanks any help is greatly appreciated

Assuming you want this

 var images = document.querySelectorAll('.content img'); if (images) { for (var i = 0; i < images.length; i++) { var element = images[i]; switch (element.dataset.src) { case '{w}': element.src = 'http://www.img.gem-flash.com/images/18592592472630941439.jpg'; break; case '{6}': element.src = 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSGsQElcTdvKt0Bu1JsgDiRKKgqlqzfUOONBMl3TqTl3mjmgTO1'; break; default: break; } } }
 <div class="content"> <img src="" data-src='{6}' alt=""> <img src="" data-src='{w}' alt=""> <img src="" data-src='{w}' alt=""> </div>

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