使用Javascript更改div中的文本

使用Javascript更改div中的文本,javascript,Javascript,我有以下代码: <div>Text: <a href="#">Fly London</a></div> 文本: 我想用JS更改文本“text:”,因此我使用了以下方法: <script> $(document).ready(function(){ $('.p-detail-info > div:contains("Text:")').text('Another text:'); });

我有以下代码:

<div>Text: <a href="#">Fly London</a></div>
文本:
我想用JS更改文本“text:”,因此我使用了以下方法:

<script>
     $(document).ready(function(){ 
          $('.p-detail-info > div:contains("Text:")').text('Another text:');
     });
</script>

$(文档).ready(函数(){
$('.p-detail-info>div:contains(“Text:”).Text(“另一个文本:”);
});
但如果我这样做,URL就会消失。如果我想保留URL,如何更改它

谢谢大家!

您必须选择文本节点,不幸的是,仅使用选择器是不可能的。选择父节点
div
,获取其第一个
childNode
(这是我们想要的文本节点),然后分配给文本节点的
textContent
,无需jQuery:

document.querySelector('div').childNodes[0]。textContent='other Text'
Text:
您必须选择文本节点,不幸的是,仅使用选择器是不可能的。选择父节点
div
,获取其第一个
childNode
(这是我们想要的文本节点),然后分配给文本节点的
textContent
,无需jQuery:

document.querySelector('div').childNodes[0]。textContent='other Text'

文本:
以下内容适用于更改文本:

$(document).ready(函数(){$('.p-detail-info>div').text('other text:');})

干杯,
快乐编码。

以下内容可以很好地更改文本:

$(document).ready(函数(){$('.p-detail-info>div').text('other text:');})

干杯,
愉快的编码。

您也可以通过添加span标记并更改span标记文本来完成此操作

$(文档).ready(函数(){
$('.p-detail-info>div>span:contains(“Text:”)).Text('other Text:');})

正文:

您也可以通过添加span标记并更改span标记文本来完成此操作

$(文档).ready(函数(){
$('.p-detail-info>div>span:contains(“Text:”)).Text('other Text:');})

正文:

我会将jQuery作为标记添加到您的问题中。我会将jQuery作为标记添加到您的问题中。