简体   繁体   English

为什么 onclick = function() 在文档准备好的内部不起作用?

[英]Why does onclick = function() does not work inside document ready?

Looking at When should I use jQuery's document.ready function?查看何时应该使用 jQuery 的 document.ready function? and Form submit button onclick does not call my JS function gives a sense that using $document.ready(function(){}) should have no harm in the code, rather it is for the good.表单提交按钮 onclick 不会调用我的 JS function给人一种感觉,使用$document.ready(function(){})应该不会对代码造成伤害,而是有益的。

But when I enter a function into the onclick method of button, the functions inside ready() statement don't get executed.但是,当我在按钮的onclick方法中输入 function 时, ready()语句中的函数不会被执行。 Why so?为什么这样?

As an example, this code does not give an alert例如,此代码不会发出警报

 $(document).ready(function(){ function f(){ alert("alert"); } }) /*function f(){ alert("alert"); } */
 <button onclick="f()"> My Button </button>

I understand that if I use the jQuery selectors to make it work, it will ( $("button").click(f) ).我知道如果我使用 jQuery 选择器使其工作,它将( $("button").click(f) )。 But why does this method fail?但是为什么这种方法会失败?

It's a problem of scope - functions declared inside functions is not accessible outside the outer functions just like variable declared inside functions:这是 scope 的问题 - 在函数内部声明的函数不能在外部函数外部访问,就像在函数内部声明的变量一样:

(function(){
    var a = 'hello';
    function b () {
        console.log(a);
    }
})();

a;   // syntax error - a is undefined
b(); // syntax error - b is undefined

You can make it work by assigning the function to a global variable:您可以通过将 function 分配给全局变量来使其工作:

var f;

$(document).ready(function(){
  f = function(){
    alert("alert");
  }
})

Anytime you declare a function you create a scope.每当您声明 function 时,您都会创建 scope。 Anything declared inside that scope is not accessible outside that scope.在 scope内部声明的任何内容都无法在 scope之外访问。

$(document).ready(function(){
  // f is only accessible to other code declared in this function.
  function f(){
    alert("alert");
  }
})

The recommended way to do this with jQuery is to assign the on click handler function in the jQuery ready function.使用 jQuery 执行此操作的推荐方法是在 jQuery 准备好的 ZC1C425268E68A85D14 中分配点击处理程序 function

HTML: HTML:

<button id="my-button-id">My Button</button>    

Javascript: Javascript:

$(document).ready(function(){
  // f is only accessible to other code declared in this function.
  function f(){
    alert("alert");
  }

  // Assign onclick here
  $('#my-button-id').on('click', f)
})

$(document).ready() gets called after all of your html has finished loading. $(document).ready() 在所有 html 完成加载后被调用。 Since you are calling the "f()" function within your html, it calls "f()" before anything in "$(document).ready()" has run yet, meaning there is no definition for "f()".由于您在 html 中调用“f()”function,因此它会在“$(document).ready()”中的任何内容运行之前调用“f()”,这意味着“f()”没有定义.

It is common now to not even use "$(document).ready()" as long as you are loading that jQuery at the end of all of your html to avoid long load times.现在通常甚至不使用“$(document).ready()”,只要您在所有 html 的末尾加载 jQuery 以避免较长的加载时间。 This will allow you to call "f()" from within your html and still have the rest of your jQuery load after your html.这将允许您在 html 中调用“f()”,并且在 ZAFC35FDC70D3C8C3CAF5C77BZA65E8800B5C6800AAD896F888B2A62AFCZ 的 jQueryA62AFCZ 加载后仍然存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM