[英]How to Display the Text using .TextContent?
我试图通过在JS中用.TextContent覆盖HTML来显示消息“嘿朋友”。 因此,不仅要说嘿! 我想说嘿朋友!
的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>
Java脚本
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');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.