我在 jquery 函数下面有这个,当我在它里面包含 click 函数时工作得很好,但是当我将它添加到 main 函数之外时它不起作用,请问我如何修复它才能有效地工作?
示例 1
(function tryBarSlot() {
function getit(){
alert('HH');
}
/* Using the onclick function here works*/
$('#play_button').click(function(event) {
getit();
});
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<button id="play_button">TRY</button>
示例 2
(function tryBarSlot() {
function getit(){
alert('HH');
}
})();
$(function(){
//getit(); /*OR*/
$('#play_button').click(function(event) {
getit();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<button id="play_button">TRY</button>
我希望这个例子起作用。
如果你想从任何地方访问它 window.getit = getit
(function tryBarSlot() {
function getit(){
alert('HH');
}
window.getit = getit;
})();
$(function(){
//getit(); /*OR*/
$('#play_button').click(function(event) {
getit();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<button id="play_button">TRY</button>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句