我无法弄清楚为什么我的JavaScript文件在链接时不会显示 [英] I can't figure out why my JavaScript file won't display when linked

查看:72
本文介绍了我无法弄清楚为什么我的JavaScript文件在链接时不会显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚这里有什么问题,而且我也没有从Javascript文件获取任何输出。我正在尝试使用src能够在索引文件外输入我的javascript文件。这只是从html文件测试标题打印标题而不打印文本变量。如果我在html文件中键入代码,它工作正常。这两个脚本都在同一个文件夹中。

 <!DOCTYPE HTML> 

< html>
< head>
< h2>
测试标题
< / h2>
< script language =JavaScriptsrc =/ slider.js>< / script>
< / head>
< / html>

以下是标记为slider.js的文件 -

 函数slider(){
var text =欢迎来到Slider Simulator 2013!;

document.write(text);
}
slider();


解决方案

如果脚本与HTML位于同一文件夹中文件,那么你可以这样做:

 < script src =slider.js>< / script> 

起始 / 表示来自根目录,并且根可能不是你所期望的。根并不意味着加载它的HTML的位置,而是文件系统或域的根。



内容必须放在< body> ,而不是其他地方。 < head> 通常用于脚本,样式,页面元数据,但不包含内容。 $ b

code> language 可以省略,因为< script> 默认运行JavaScript。


I can't figure out what's wrong here and I'm not getting any output from the Javascript file. I am trying to use src to be able to type my javascript file outside of the index file. This just prints the header from the html file "A test heading" without printing the text variable. If I type the code within the html file it works fine. Both scripts are in the same folder.

<!DOCTYPE HTML>

<html>
<head>
    <h2>
    A Test Heading
    </h2>
    <script language = "JavaScript" src="/slider.js"></script>
</head>
</html>

Here's the file labeled slider.js -

function slider(){  
var text = "Welcome to Slider Simulator 2013!";

document.write(text);
}
slider();

解决方案

If the script is in the same folder as the HTML file, then you can just do:

<script src="slider.js"></script>

The starting / means "from the root", and the root might not be what you'd expect. Root does not mean the location of the HTML that loaded it, but the root of the file system or the domain.

Content must be placed inside the <body>, and not anywhere else. <head> is usually for scripts, styles, page metadata, but not the content.

Also, language can be omitted since <script> run JavaScript by default anyway.

这篇关于我无法弄清楚为什么我的JavaScript文件在链接时不会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆