![](/img/trans.png)
[英]jquery $(document).one ('ready', function () {…} does not work
[英]jQuery document ready and keypress to call one function
我試圖將其作為文檔准備好功能,當按下Enter鍵時觸發該功能,但我不斷收到錯誤消息“ enableEnter未定義”。
$(document).ready(enableEnter());
$('#formPartOverride').keypress(function enableEnter(event) {
if (event.keyCode == 13) {
event.preventDefault();
ezpConsole.partOverride.retrieveParts();
}
});
這是實現此目標的正確方法:
$(document).ready(function () {
// When the DOM is ready, attach the event handler.
$('#formPartOverride').keypress(function (event) {
enableEnter(event);
});
});
// enableEnter is accessible in the whole page scope.
function enableEnter(event) {
if (event.keyCode == 13) {
event.preventDefault();
ezpConsole.partOverride.retrieveParts();
}
}
在enableEnter()
函數之外創建函數enableEnter()
keypress1 function - it is only defined in the scope of the
enableEnter()
函數的作用enableEnter()
keypress1 function - it is only defined in the scope of the
function, so will not be available to anything above that specific scope. You could encase the entire script inside the
function, so will not be available to anything above that specific scope. You could encase the entire script inside the
$(document).ready()`函數中,例如:
$(document).ready(function() {
$('#formPartOverride').keypress(function enableEnter(event) {
if (event.keyCode == 13) {
event.preventDefault();
ezpConsole.partOverride.retrieveParts();
}
});
});
這將等待文檔准備好使隨附的腳本可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.