gpt4 book ai didi

javascript - 如何使用.TextContent显示文本?

转载 作者:行者123 更新时间:2023-12-02 14:38:08 24 4
gpt4 key购买 nike

我试图通过在 JS 中用 .TextContent 覆盖 HTML 来显示消息嘿 friend 。所以不要只是说嘿!我想让它说嘿 friend !

HTML

<!doctype html>


<html lang="en">
<head>
<meta charset="utf-8">
<title>friends</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

<!-- This links to the js code specific for this page -->
<script src="multiwrites.js"></script>

<script src="multiwrites2.js"></script>
</head>
<body>
<div id="title">Hey!</div>
</body>
</html>

Javascript

var message;
message = 'Hey Friends';

var elName = document.getElementById('title');
elName.textContent = message;

最佳答案

您需要在它们将访问的 DOM 节点之后包含脚本标签:

<!DOCTYPE html>
<html>
<head>
<title>Friends</title>
</head>
<body>
<div id="title">hello</div>
<script type="text/javascript">
var el = document.getElementById('title');
el.textContent = "hello world";
</script>
</body>
</html>

或者等待 DOM“准备好”,我可以看到您已经包含了 jQuery,因此可以通过以下方式完成:

jQuery(function($) {
// In here the whole DOM will be "ready"
// Using jQuery API instead of native DOM API.
$('#title').text('hello world');
});

关于javascript - 如何使用.TextContent显示文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37261274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com