英文:
Solving a simple JS script
问题
可以有人帮我修复这个问题吗?我进行了研究,但无法使其工作
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-html -->
<h1 id="fruit">apples</h1>
<h2>I like<span id="sentence"></span></h2>
<script>
document.getElementById("sentence").innerHTML = fruit;
const sentence = document.getElementById("fruit");
</script>
<!-- 结束代码片段 -->
基本上,我想要h2显示"I like apples",使用h1的id。
英文:
Can anyone help me fix this? I did research but can't make it work
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<h1 id="fruit">apples</h1>
<h2>I like<span id="sentence"></span></h2>
<script>
document.getElementById("sentence").innerHTML = fruit;
const sentence = document.getElementById("fruit");
</script>
<!-- end snippet -->
Basically I want the h2 to display "I like apples", using the id of h1
Thanks.
答案1
得分: 0
你只需要将句子的innerHTML设置为水果的innerHTML。
英文:
You just have to set the innerHTML of sentence to the innerHTML of fruit.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<body>
<h1 id="fruit">apples</h1>
<h2>I like <span id="sentence"></span></h2>
<script>
document.getElementById("sentence").innerHTML = document.getElementById("fruit").innerHTML;
</script>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论