【问题标题】:google.setOnLoadCallback not able to get the document elementsgoogle.setOnLoadCallback 无法获取文档元素
【发布时间】:2016-02-09 23:44:03
【问题描述】:

我正在尝试使用 JQuery 通过 corechart 获得一些图形功能,但是一旦加载了可视化 API,我就无法获得任何文档元素。下面是我的示例:

$(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 元素)之后才加载可视化包。请指教

【问题讨论】:

  • 加载 google 库的类似,杀死文档元素及其下的所有内容,检查 google.load("visualization" 的行为

标签: jquery charts google-visualization


【解决方案1】:

我认为这可能与setOnLoadCallback有关。
但是,您可以在load 语句中添加对回调的引用。

$(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 已经被加载了,可能有些东西被搞砸了……

【讨论】:

    猜你喜欢
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 2021-03-06
    • 2011-07-13
    • 2021-12-16
    • 2019-05-24
    相关资源
    最近更新 更多