google.setOnLoadCallback无法获取文档元素 [英] google.setOnLoadCallback not able to get the document elements

查看:223
本文介绍了google.setOnLoadCallback无法获取文档元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用JQuery获得一些图表功能,但是我不能在可视化API加载后得到任何文档元素。下面是我的示例示例:

I am trying to get some graph functionalities with corechart using JQuery but I am not able to get any document elements once the visualization APIs are loaded. Below is my sample example:

$(document).ready(function() {
  if ($("#myId").length > 0) { 
    google.load("visualization", "1.1", {
      packages: ["corechart"]
    });
    google.setOnLoadCallback(function() {
      if ($("#myId").length > 0) {
        alert("Found a match!!");
      }else{
        alert("Not found!!");
      }
    });
  }
});

<html>

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  </head>

  <body>
    <h1>Hello Plunker!</h1>
    <div id="myId">Hello from Div!!</div>
  </body>

</html>

警报在我的if块被执行。我只是在检查#myId之后加载可视化包,但在回调中丢失(而不是所有的html元素)。请咨询

I want the alert in my if block to be executed. I am loading the visualization packages only after the check for #myId but its lost(rather all the html elements) in the call back. Please advice

推荐答案

我认为它可能与 setOnLoadCallback

不过,您可以在 load 语句中添加对回调的引用。

I think it may have something to do with setOnLoadCallback.
However, you can add a reference to the callback in the load statement.

$(document).ready(function(){
  if ($("#myId").length > 0) {
    google.load('visualization', '1', {
      'packages': ['corechart'],
      'callback': function(){
        if ($("#myId").length > 0) {
          alert("Found a match!!");
        }else{
          alert("Not found!!");
        }
      }
    });    
  }
});

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  </head>

  <body>
    <h1>Hello Plunker!</h1>
    <div id="myId">Hello from Div!!</div>
  </body>

</html>

> setOnLoadCallback 会执行双重任务,而不仅仅是等待 google.load ...

,而且还有文档

,因为文档弄脏了...

I think setOnLoadCallback pulls double duty and not only waits for google.load...
but also the document
since document has already been loaded, maybe something gets fouled up...

这篇关于google.setOnLoadCallback无法获取文档元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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