使用jQuery $(document).ready()的google.setOnLoadCallback,可以混合使用吗? [英] google.setOnLoadCallback with jQuery $(document).ready(), is it OK to mix?

查看:136
本文介绍了使用jQuery $(document).ready()的google.setOnLoadCallback,可以混合使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Ajax API,他们建议我使用 google.setOnLoadCallback()来做与API有关的各种事情,但我也在使用jQuery的 $(document).ready()执行与Google API无关的其他JS操作。

I'm using Google Ajax API and they suggest I use google.setOnLoadCallback() to do various things related to their API but I'm using also jQuery's $(document).ready() to do other JS things, not related to Google API.

是否安全将这两种方法混合在一个文档中?我没有注意到任何问题,但我认为这是一个规模问题。

Is it safe to mix these two approaches in one document? I did not notice any problems yet but I suppose it's a matter of scale.

推荐答案

你几乎必须这样做:

google.setOnLoadCallback(function() {
  $(function() {
    // init my stuff
  });
});

你不能做 $(文件).ready()没有 $ (jQuery对象)可用,因此需要进入回调。并且您无法确定文档在回调中是否已准备就绪,因此您必须执行 ready()

You can't do $(document).ready() without $ (the jQuery object) being available, so that needs to go inside the callback. And you can't be sure the document is ready inside the callback, so you have to do ready() too.

这篇关于使用jQuery $(document).ready()的google.setOnLoadCallback,可以混合使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆