Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改跨距';s文本,使儿童不受影响_Javascript_Jquery - Fatal编程技术网

Javascript 更改跨距';s文本,使儿童不受影响

Javascript 更改跨距';s文本,使儿童不受影响,javascript,jquery,Javascript,Jquery,如果我有下面的标记,我如何使用javascript或jQuery更改父范围内的文本 <td class="cartItemTotal"> <span> <span class="currency_sign">£</span> 25.00 </span> </td> 注意:“$this”指的是表数据元素所在的当前。与标记相比,您的选择器没有意义,但是如果您可以访问.total>

如果我有下面的标记,我如何使用javascript或jQuery更改父范围内的文本

<td class="cartItemTotal">
    <span>
        <span class="currency_sign">£</span>
        25.00
    </span>
</td>

注意:“
$this
”指的是表数据元素所在的当前

与标记相比,您的选择器没有意义,但是如果您可以访问
.total>span
,您可以获取DOM元素
[0]
,并使用
.lastChild

$(".total > span").each(function(i, el) {
    console.log(el.lastChild.data);
});
$this.find('.cartItemTotal > span')[0].lastChild.data = itemTotal.toFixed(2);
演示:


所以您的上一个代码示例就快到了。只需要使用
.lastChild
而不是
.firstChild

$(".total > span").each(function(i, el) {
    console.log(el.lastChild.data);
});
$this.find('.cartItemTotal > span')[0].lastChild.data = itemTotal.toFixed(2);

与标记相比,您的选择器没有意义,但是如果您可以访问
.total>span
,则可以获取DOM元素
[0]
,并使用
.lastChild

$(".total > span").each(function(i, el) {
    console.log(el.lastChild.data);
});
$this.find('.cartItemTotal > span')[0].lastChild.data = itemTotal.toFixed(2);
演示:


所以您的上一个代码示例就快到了。只需要使用
.lastChild
而不是
.firstChild

$(".total > span").each(function(i, el) {
    console.log(el.lastChild.data);
});
$this.find('.cartItemTotal > span')[0].lastChild.data = itemTotal.toFixed(2);

jQuery在textNodes方面做得并不好,但是使用本机
nextSibling
可以在
.currency\u sign
元素之后获得textNode:

$('.currency_sign').get(0).nextSibling.nodeValue = 'new value';


我正在使用
get(0)
获取集合中的第一个本机DOM元素。

jQuery对textNodes不太好,但是使用本机
nextSibling
可以在
元素之后获取textNode

$('.currency_sign').get(0).nextSibling.nodeValue = 'new value';

我正在使用
get(0)
获取集合中的第一个本机DOM元素。

您可以使用contents()和filter()来获取textNodes

var node = $(".cartItemTotal>span").contents().filter(function() {
        return this.nodeType == 3;
    });
node.last().replaceWith( document.createTextNode( "1.99" ));
运行Fiddle:

您可以使用contents()和filter()来获取textNodes

var node = $(".cartItemTotal>span").contents().filter(function() {
        return this.nodeType == 3;
    });
node.last().replaceWith( document.createTextNode( "1.99" ));

Running Fiddle:

我没有在你的HTMLApologies@DavidStarkey-modified中看到
cartItemTotal
。你想过取消你的跨度嵌套吗
£
25.00
@MikeB-我有,但我使用的CMS不允许这样做。我没有在你的HTMLApologies@DavidStarkey-修订版中看到
cartItemTotal
。你考虑过取消你的跨度嵌套吗
25.00
@MikeB-我有,但我使用的CMS不允许这样做。@DavidStarkey:因为它是
.total>span
元素的最后一个子元素。虽然其他元素显然在各自的小提琴中工作,但这是在我的页面中实际工作的唯一答案。奇怪,但却是真的。@DavidStarkey:因为它是
.total>span
元素的最后一个子元素。虽然其他元素显然在各自的小提琴中工作,但这是在我的页面中实际工作的唯一答案。奇怪,但却是真的。