开发者

jquery replace text with image, help

开发者 https://www.devze.com 2023-03-05 11:39 出处:网络
(\"*\").each(function () { if ($(this).children().length == 0) { $(this).text($(this).text().replace(\'basketball\',\'test开发者_如何学JAVA\'));
("*").each(function () { 
   if ($(this).children().length == 0) {
       $(this).text($(this).text().replace('basketball','test开发者_如何学JAVA')); 
   } 
});

i'm only able to change the text to another string of text, but how can I pass an image?


("*").each(function () { 
   if ($(this).children().length == 0) {
      var newHTML = $(this).html().replace('basketball','<img src = "image.jpg" />');
      $(this).html(newHTML);
   } 
});

EDIT

My mistake. I thought you wanted to replace the entire element. Check out my updated answer.


You need to modify the html() rather than the text().

You can simplify your code to

$("*").filter(function() { return !$(this).children().length; })
      .html(function(index, old) { return old.replace('basketball', '<img ... />'); });


I'd reccomend JQIR for this. first replace "basketball" with "<span class="jqir">basketball</span>" as you are doing above. Then run the following:

$(".jqir").jQIR("png", "images/");

This assumes that you have an image images/basketball.png. You can adjust as needed.

0

精彩评论

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