admin管理员组文章数量:1316662
<p onclick="play()">abc</p>
js
function play(){
do something;
}
The above works if js code is in the same file as p
But saying:
<script src="index.js"></script>
index.js
$(document).ready( function() {
function play() {
do something;
}
});
What i get is ReferenceError: play is not defined
Other functions, except play()
works well.
<p onclick="play()">abc</p>
js
function play(){
do something;
}
The above works if js code is in the same file as p
But saying:
<script src="index.js"></script>
index.js
$(document).ready( function() {
function play() {
do something;
}
});
What i get is ReferenceError: play is not defined
Other functions, except play()
works well.
-
1
Have you tried removing the
$(document).ready(function() {});
if the javascript is within an external file? – Nunners Commented Oct 15, 2013 at 7:48 - 1 thanks to everyone. solved – qadenza Commented Oct 15, 2013 at 7:51
3 Answers
Reset to default 4$(document).ready(function(){
function play(){
do something;
}
});
play() function is local
to $(document).ready(function(){
not global
Don't wrap your play function in $(document).ready(function(){
to keep it's scope global .
function play(){
do something;
}
Read Global and Local and Private Functions (Javascript)
and What is the scope of variables in JavaScript?
This is due to scope.
play()
isn't visible at that level, due to it being wrapped inside your $(document).ready
function.
No need to place it in $(document).ready( function() {..}
just mention it in your index.js
as
function play(){
do something;
}
remove $(document).ready( function() {
本文标签: javascriptonclick and external js fileStack Overflow
版权声明:本文标题:javascript - onclick and external js file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742005814a2411930.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论