更新“文本". jQuery的节点 [英] Updating "text" nodes with jQuery

查看:91
本文介绍了更新“文本". jQuery的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似html的

<label><input type="radio" /> Lorem</label>

将"Lorem"更改为"Ipsum"的最佳方法是什么?这些不起作用:

What's the best way to change "Lorem" to "Ipsum"? These don't work:

// nope:
$('input').next().text('Ipsum'); 

// also nope:
var text = $('input').parent()[0].childNodes[1];
$(text).text('Ipsum');

我可以轻松地将html更改为:

I can easily change the html to:

<label><input type="radio" /><span> Lorem</span></label>

或直接使用replaceWholeText DOM方法:

or use the replaceWholeText DOM method directly:

$('input').parent()[0].childNodes[1].replaceWholeText(' Ipsum');

或其他任何东西,但是我想知道是否有一种干净的jQuery方式来做到这一点.

or any number of other things, but I'm wondering if there's a clean jQuery way to do this.

推荐答案

$('label').contents().last().replaceWith("Ipsum");

但是,我个人会使用nodeValue.

However, I personally would use nodeValue.

希望这会有所帮助!

演示: http://jsfiddle.net/dirtyd77/SLGdE/25/

这篇关于更新“文本". jQuery的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆