无法从html文件调用js文件 [英] Trouble calling a js file from an html file

查看:61
本文介绍了无法从html文件调用js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,只要.js

文件没有使用函数,这是一件简单明了的事情。当我在

js文件中尝试使用函数的脚本时,我无法使其工作(尽管它在html

文件中工作正常)。我猜这里有一些语法或惯例我不是很好。


我最需要的是一个例子。例如,有人可以使用

alert命令编写一个js文件,以保持简单。也许是这样的:


var name = prompt(''你的名字是什么?'')

document.write(''Hello''+ name )


然后,从html文件中调用此js文件。在你测试它并确保它确定它有效之后,请把这两个部分发给我。


我看了但是找不到任何工作这个

特定问题的例子或讨论。


~J


***通过Developersdex发送 http://www.developersdex.com ***

Yes, this is a simple and straightforward thing to do as long as the .js
file doesn''t use a function. When I try a script with a function in a
js file I can''t get it to work (though it works fine within the html
file). I''m guessing that there''s some syntax or convention I''m not
doing right.

What I most need is an example. Can someone write a js file with an
alert command, for example, to keep it simple. Perhaps something like:

var name = prompt(''What is your name?'')
document.write(''Hello '' + name)

Then, call this js file from an html file. After you test it and make
sure it works, please send me the two parts.

I''ve looked but can''t find any working examples or discussions of this
particular issue.

~ J

*** Sent via Developersdex http://www.developersdex.com ***

推荐答案

Jake j说:
Jake j said:
我最需要的是一个例子。例如,有人可以使用
警告命令编写一个js文件,以保持简单。
What I most need is an example. Can someone write a js file with an
alert command, for example, to keep it simple.




你不能调用文件,你包括它。


这是一个工作示例:

< html>

< body>

< script type =" text / javascript"

src =" http://www.azphx.com/dhtml/tmp/helloAlert.js">< / script> ;

< / body>

< / html>


文件helloAlert.js包含一行:

alert(" hello,world!");


如果您发布一个简单的示例
$ b $可能会更有帮助b *不能为你工作。



You don''t call a file, you include it.

Here''s a working example:
<html>
<body>
<script type="text/javascript"
src="http://www.azphx.com/dhtml/tmp/helloAlert.js"></script>
</body>
</html>

The file helloAlert.js contains a single line:
alert("hello, world!");

It might be more helpful if you post a simple example that
*doesn''t* work, for you.


Jake j写道:
我最需要的是一个例子。例如,有人可以使用
警报命令编写js文件,以保持简单。也许类似于:
What I most need is an example. Can someone write a js file with an
alert command, for example, to keep it simple. Perhaps something like:




file test.html

< html>

< script type =" ;文本/ JavaScript的" src =" test.js">< / script>

< a href ="#" onclick =" alert(foo);"> test external js< / a>

< / html>


file test.js < br $>
foo =''Hello Jake'';

其他


file test_js.html

< ; html>

< script type =" text / javascript" src =" test_js.js">< / script>

< a href ="#" onclick =" result();"> test external js< / a>

< form>

结果=<输入类型=文字名称=结果> ;

< / form>

< / html>


file test_js.js

函数result(){

document.forms [0] .result.value = prompt(''你叫什么名字?'');

}


-

Stephane Moriaux et son [moins] vieux Mac



file test.html
<html>
<script type="text/javascript" src="test.js"></script>
<a href="#" onclick="alert(foo);">test external js</a>
</html>

file test.js
foo = ''Hello Jake'';
other

file test_js.html
<html>
<script type="text/javascript" src="test_js.js"></script>
<a href="#" onclick="result();">test external js</a>
<form>
Result = <input type=text name=result>
</form>
</html>

file test_js.js
function result() {
document.forms[0].result.value=prompt(''What is your name?'');
}

--
Stephane Moriaux et son [moins] vieux Mac




ASM写道:

....


ASM wrote:
....
file test_js.js
功能结果(){
document.forms [0 ] .result.value =提示(''你的名字是什么?'');
}
file test_js.js
function result() {
document.forms[0].result.value=prompt(''What is your name?'');
}




你不能使用javascript函数表单输入值。你是混合javascript和html的
。 Javascript必须在< script>内标签。



You can''t use a javascript function as a form input value. You are
mixing javascript and html. Javascript has to be inside <script> tags.


这篇关于无法从html文件调用js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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