我尝试更改 span 标签内的部分文本 我试过这个:
<script>
$(document).ready(function(){
var toreplace= $(".poly-items__price span");
toreplace = toreplace.replace("change","<p>new text</p>");
$(".poly-items__price span").html(toreplace);
});
</script>
这是一个 html:
<div class="poly-items__price">
<span>do not change change</span>
</div>
这是我用过的例子: http://jsfiddle.net/Scoobler/a9cvx/4/ , http://jsfiddle.net/bwhfD/
但是没用 你能告诉我我错过了什么吗 谢谢
最佳答案
要从 $(".poly-items__price span")
获取文本,需要调用 .text()
(或 .html()
如果你关心 span 内的 html 标签)
$(document).ready(function(){
var toreplace= $(".poly-items__price span").text();
toreplace = toreplace.replace("change","<p>new text</p>");
$(".poly-items__price span").html(toreplace);
});
如果你想替换所有“更改”使用:
toreplace = toreplace.replace(/change/g,"<p>new text</p>");
这是一些 fiddle http://jsfiddle.net/qsbdg4po/1
编辑
如果您有多个 .poly-items__price
跨度,请使用每个循环:
$('.poly-items__price span').each(function() {
var toreplace = $(this).html();
toreplace = toreplace.replace("change","<p>new text</p>");
$(this).html(toreplace);
});
关于javascript - 更改 span jquery 中的部分文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33166954/