如何使用javascript从<div>的文本childNode访问特定文本?</div>

时间:2012-06-23 05:11:20

标签: javascript regex

<html>
<head></head>
<body>
<div class="mtb-desc">
    some contentsome contentsome contentsome  contentsome contentsome contentsome contentsome content.

    some contentsome contentsome contentsome entsome contentsome contentsome contentsome contentsome.

    NO : 212122Name is Jone</div>
<span class="mtb-price">
<label Class="mtb-ofr"><b class="lb1"></b>WWWW</label></span>
<script>
var eg1 = document.querySelectorAll('.mtb-desc')[0].childNodes[0].split(null)[1].nodeValue;
var eg2 = document.querySelectorAll('.mtb-ofr')[0].childNodes[1].nodeValue = eg1;
alert(eg2);
</script>
</body>
</html>

在上面的代码中我想用文本替换WWWW名称是Jonas。那么如何从该文本节点访问“Name is Jonas”文本?

1 个答案:

答案 0 :(得分:1)

要匹配结束的非数字文本并将其插入WWWW,请执行以下操作:

var content = document.querySelectorAll('.mtb-desc')[0].innerText;
var insertion = content.match(/[^0-9]+$/)[0];
document.querySelectorAll('.mtb-ofr')[0].innerText = insertion;