jquery用图像替换文本,帮助
("*").each(function () { if ($(this).children().length == 0) { $(this).text($(this).text().replace('basketball','test')); } });
我只能将文本更改为另一个文本字符串,但如何传递图像?
("*").each(function () { if ($(this).children().length == 0) { var newHTML = $(this).html().replace('basketball',''); $(this).html(newHTML); } });
编辑
我的错。 我以为你想要替换整个元素。 看看我更新的答案。
您需要修改html()
而不是text()
。
您可以将代码简化为
$("*").filter(function() { return !$(this).children().length; }) .html(function(index, old) { return old.replace('basketball', ''); });
我会为此推荐JQIR 。 首先用"basketball"
替换"basketball"
,如上所述。 然后运行以下命令:
$(".jqir").jQIR("png", "images/");
这假设你有一个图像images / basketball.png。 您可以根据需要进行调整。