Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.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更改H1标记的内容?_Javascript_Html - Fatal编程技术网

使用Javascript更改H1标记的内容?

使用Javascript更改H1标记的内容?,javascript,html,Javascript,Html,关于这些话题,我知道的非常少,但我希望你能帮助我。基本上,我的页面的编码是自动设置的,我根本无法更改它,但我可以将代码添加到其标题部分 现在,我的问题是。。。该页面已具有H1标记,但使用不正确。所以,我想更改H1标记中的文本,但我需要动态地进行更改。 使用Javascript可以做到这一点吗?怎么做 简单地说,我想动态更改: <h1>tag here</h1> 在这里标记 致: 此处已更正标记 使用getElementsByTagName 函数更改(){ var h

关于这些话题,我知道的非常少,但我希望你能帮助我。基本上,我的页面的编码是自动设置的,我根本无法更改它,但我可以将代码添加到其标题部分

现在,我的问题是。。。该页面已具有H1标记,但使用不正确。所以,我想更改H1标记中的文本,但我需要动态地进行更改。 使用Javascript可以做到这一点吗?怎么做

简单地说,我想动态更改:

<h1>tag here</h1>
在这里标记
致:

此处已更正标记

使用getElementsByTagName

函数更改(){
var h=document.getElementsByTagName(“h1”)[0];
h、 innerText=“此处已更正标记”;
}
在这里标记

单击使用getElementsByTagName

函数更改(){
var h=document.getElementsByTagName(“h1”)[0];
h、 innerText=“此处已更正标记”;
}
在这里标记

单击
我不确定您的情况,因此我不知道为什么您只能将代码添加到head标记。但是,您可能会在head标记中添加一个script标记

大概是这样的:

<script>
    document.addEventListener('DOMContentLoaded', e => {
        // select the h1 by a class, id, or even tag name
        // in this example, i'm just selecting it by tag name
        const h1 = document.querySelector("h1")

        h1.innerText = "Correct Text"
    })
</script>

document.addEventListener('DOMContentLoaded',e=>{
//通过类、id甚至标记名选择h1
//在这个例子中,我只是通过标记名来选择它
const h1=document.querySelector(“h1”)
h1.innerText=“正确的文本”
})
编辑:OP询问如何按标记更改多个h1

如果要更改多个h1,可以改为:

<script>
    document.addEventListener('DOMContentLoaded', e => {
        const h1s = document.querySelectorAll("h1")

        // This will change the innerText for the first h1 on your page
        h1s[0].innerText = "Correct Text"

        // This will change the innerText for the 2nd h1 on your page, etc.
        h1s[1].innerText = "Some other correct text"
    })
</script>

document.addEventListener('DOMContentLoaded',e=>{
const h1s=document.queryselectoral(“h1”)
//这将更改页面上第一个h1的innerText
h1s[0]。innerText=“正确的文本”
//这将更改页面上第二个h1的内部文本,等等。
h1s[1]。innerText=“其他一些正确的文本”
})
)

这将在h1加载后更改其文本

通过向事件“DOMContentLoaded”的文档中添加事件侦听器,可以确保在脚本尝试更改其内容之前h1将存在

下面是一个工作示例:


文件
document.addEventListener('DOMContentLoaded',e=>{
const h1=document.querySelector(“h1”)
h1.innerText=“正确的文本”
})
错误文本

我不确定您的情况,所以我不知道为什么您只能在head标签中添加代码。但是,您可能会在head标记中添加一个script标记

大概是这样的:

<script>
    document.addEventListener('DOMContentLoaded', e => {
        // select the h1 by a class, id, or even tag name
        // in this example, i'm just selecting it by tag name
        const h1 = document.querySelector("h1")

        h1.innerText = "Correct Text"
    })
</script>

document.addEventListener('DOMContentLoaded',e=>{
//通过类、id甚至标记名选择h1
//在这个例子中,我只是通过标记名来选择它
const h1=document.querySelector(“h1”)
h1.innerText=“正确的文本”
})
编辑:OP询问如何按标记更改多个h1

如果要更改多个h1,可以改为:

<script>
    document.addEventListener('DOMContentLoaded', e => {
        const h1s = document.querySelectorAll("h1")

        // This will change the innerText for the first h1 on your page
        h1s[0].innerText = "Correct Text"

        // This will change the innerText for the 2nd h1 on your page, etc.
        h1s[1].innerText = "Some other correct text"
    })
</script>

document.addEventListener('DOMContentLoaded',e=>{
const h1s=document.queryselectoral(“h1”)
//这将更改页面上第一个h1的innerText
h1s[0]。innerText=“正确的文本”
//这将更改页面上第二个h1的内部文本,等等。
h1s[1]。innerText=“其他一些正确的文本”
})
)

这将在h1加载后更改其文本

通过向事件“DOMContentLoaded”的文档中添加事件侦听器,可以确保在脚本尝试更改其内容之前h1将存在

下面是一个工作示例:


文件
document.addEventListener('DOMContentLoaded',e=>{
const h1=document.querySelector(“h1”)
h1.innerText=“正确的文本”
})
错误文本

文件
window.onload=function(){
设h1=document.getElementsByTagName('h1')
h1[0]。innerText=“此处已更正标记”
///h1[1]。innerText=“更正了此处的标记”
////......
}
标记在这里

文件
window.onload=function(){
设h1=document.getElementsByTagName('h1')
h1[0]。innerText=“此处已更正标记”
///h1[1]。innerText=“更正了此处的标记”
////......
}
标记在这里

我不能。如上所述,我无法更改原始代码,这意味着我无法向其添加id…@flyingaduck updatedI无法。如上所述,我无法更改原始代码,这意味着我无法向其添加id…@Flying驴更新我的情况有点奇怪,很抱歉。您的代码可以替换第一个标记-谢谢但是,是否有可能替换它的所有实例,或者甚至添加一个新实例呢?啊,这有点棘手。是否要将所有h1更改为具有相同的值?或者你希望每个h1都有不同的值吗?看起来-我正在测试这个,看看什么最适合当前的代码-我想改变其中一个(目前有两个),甚至添加一个新的。你能复制和粘贴现有h1的HTML以便我能看到我将如何选择它们吗?本质上,事情是这样的:

我的情况有点奇怪,很抱歉。您的代码可以替换第一个标记-谢谢但是,是否有可能替换它的所有实例,或者甚至添加一个新实例呢?啊,这有点棘手。是否要将所有h1更改为具有相同的值?或者你希望每个h1都有不同的值吗?看起来-我正在测试这个,看看什么最适合当前的代码-我想改变其中一个(目前有两个),甚至添加一个新的。你能复制和粘贴现有h1的HTML以便我能看到我将如何选择它们吗?本质上,它是这样的: