Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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 更改子节点中的文本_Javascript_Jquery_Html - Fatal编程技术网

Javascript 更改子节点中的文本

Javascript 更改子节点中的文本,javascript,jquery,html,Javascript,Jquery,Html,我有以下标记: <label class="radio"> <input class="radio" type="radio" name="month_type" value="0" checked="checked"> On day 5 </label> 运气不好。试试这个:- JS:- $("input[name='month_type']").parent().html(function(){ return $(this).html().rep

我有以下标记:

<label class="radio">
<input class="radio" type="radio" name="month_type" value="0" checked="checked">
On day 5
</label>
运气不好。

试试这个:- JS:-

$("input[name='month_type']").parent().html(function(){
    return $(this).html().replace("On day 5","On day 6");
});
试一试

试试这个

$("#textchange").click(function(){
$('input[name=month_type]').parent().html('<input class="radio" type="radio" name="month_type" value="0" checked="checked">On day 6');
});
$(“#文本更改”)。单击(函数(){
$('input[name=month_type]')。parent().html('On day 6');
});

要更改文本,最好尝试javascript本身而不是jQuery。您可能知道标签有2个子节点。第一个是nodeType=1,最后一个是nodeType=3。试试这个,你会得到你想要的:

$("input[name='month_type']").parent()[0].lastChild.textContent = "On day 6";

实际上,标签有3个子节点,第一个是空文本节点,第二个是输入节点。
$("#textchange").click(function(){
$('input[name=month_type]').parent().html('<input class="radio" type="radio" name="month_type" value="0" checked="checked">On day 6');
});
$("input[name='month_type']").parent()[0].lastChild.textContent = "On day 6";