admin管理员组文章数量:1402858
Guys i have this function inside my script.js:
$(document).ready(function() {
function alert() {
alert('AAAAAAAA');
}
});
And i am trying to call here in my index.html:
$('.something').on('click', function() {
e.preventDefault();
alert();
});
But is showing my this error - alert is not defined.
But when i take off the document ready in the external script, the click handler will work. Why is that?
The document ready is creating a separate scope?
Guys i have this function inside my script.js:
$(document).ready(function() {
function alert() {
alert('AAAAAAAA');
}
});
And i am trying to call here in my index.html:
$('.something').on('click', function() {
e.preventDefault();
alert();
});
But is showing my this error - alert is not defined.
But when i take off the document ready in the external script, the click handler will work. Why is that?
The document ready is creating a separate scope?
Share Improve this question edited Feb 7, 2015 at 21:10 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Feb 7, 2015 at 21:01 user4520762user4520762 6- 3 You shouldn't use "alert" as name for a custom function, since alert is already a built-in function in javascript. Try to call it differently and don't forget to add "e" as a parameter in your click callback function. – Adrian Bolonio Commented Feb 7, 2015 at 21:04
- You are missing a parameter in the call to alert() in $(".something", .... – castor Commented Feb 7, 2015 at 21:05
- @AdriánBolonio this is just an example, the real function is more plex them that, but the idea is the same. – user4520762 Commented Feb 7, 2015 at 21:07
-
It's not the
$(document).ready()
that is creating the scope, but thefunction(){ }
! – Bergi Commented Feb 7, 2015 at 21:09 - Please ask only one question per post, I've removed the second one. Ask another one if you don't find your answer in this blog post - yes, your version is usually better than your teacher's. – Bergi Commented Feb 7, 2015 at 21:12
3 Answers
Reset to default 6Using $(document).ready()
creates a new function scope (note the function()
after the .ready
), so when you call
$(document).ready(function() {
function alert() {
alert('AAAAAAAA');
}
});
alert
is only defined within the document.ready
block. There are two ways to solve this issue:
Define the function outside of the
document.ready
block:function customAlert() { alert('AAAAAAAA'); }
Attach the function to the
window
object:$(document).ready(function() { window.customAlert = function() { alert('AAAAAAAA'); }; });
Include the click event into the document.ready
Check it here http://jsfiddle/fbrcm45q/1/
$(document).ready(function() {
function showAlert() {
alert('AAAAAAAA');
}
$('.something').on('click', function(e) {
e.preventDefault();
showAlert();
});
});
First of all e.preventDefault is a function so you have to add braces at the end:
e.preventDefault()
Second alert is a function in javascrpt, so you need to rename your function to something else, for example:
$(document).ready(function() {
function special_alert() {
alert('AAAAAAAA');
}
});
and:
$('.something').on('click', function() {
e.preventDefault();
special_alert();
});
本文标签: javascript(document)ready is a global scopeStack Overflow
版权声明:本文标题:javascript - (document).ready is a global scope? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744292749a2599194.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论