我在单独的 Sample.js 文件中有这个脚本:
function MyPrint(text)
{
document.write(text);
}
我有以下 HTML 页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Silly example</title>
</head>
<body>
<div>
<script type="text/javascript" src="JavaScript/Sample.js">
MyPrint("Hello silly world!");
</script>
</div>
</body>
</html>
最终结果是文本“Hello silly world!”未打印在页面上。我应该怎么做才能使这项工作?如果可能的话,我不想将脚本标签移到头部。谢谢。
最佳答案
我认为 src 标签覆盖了里面的任何内容。
尝试以下操作:
<script type="text/javascript" src="JavaScript/Sample.js"></script>
<script type="text/javascript">
MyPrint("Hello silly world!");
</script>
关于javascript - 世界上最简单的 JavaScript 都行不通!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4010376/