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

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

问题描述

我正在尝试使用 JQuery 通过 corechart 获取一些图形功能,但是一旦加载了可视化 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...
还有document
由于 document 已经被加载,可能有些东西被搞砸了...

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天全站免登陆