无法在回调函数中使用getElementById

时间:2013-08-26 21:54:01

标签: javascript jquery google-chrome google-chrome-extension callback

我在Chrome API的getAllFrames中有一个回调函数:

chrome.webNavigation.getAllFrames({tabId: tabArray[0].id},getAllFramesCallback);

我无法使用回调函数getAllFramesCallback从html文件tabStructure.html中查找元素ID,该文件是使用以下命令创建的:

chrome.windows.create({url:"tabStructure.html", type:'popup', height:450, width:600, left:1500, focused:true});

tabStructure.html:

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>TabStructure</title>
  <link rel="stylesheet" href="./jquery-ui-1.10.3/themes/base/jquery-ui.css" />
  <script src="./jquery-ui-1.10.3/jquery-1.9.1.js"></script>
  <script src="./jquery-ui-1.10.3/ui/jquery-ui.js"></script>
  <link rel="stylesheet" href="./jquery-ui-1.10.3/demos/demos.css"/>
  <script src="background.js"></script>

  <script src="tabStructure.js"></script>

</head>
<body>

<div id="tabs">
  <h3 id = "tabId">Tab ID</h3>
  <ul>
    <li><a href="#frame0">Main Frame</a></li>
  </ul>
  <div id="frame0">
    <p>abc</p>
    <ul>
      <li><a href="#frame1">sub_Frame</a></li>
    </ul>
    <div id="frame1">
    </div>
  </div>
</div>

</body>
</html>

background.js

function getAllFramesCallback(framesList){
    for ( i = 0 ; i < framesList.length ; i++){
        drawOnPage(framesList[i]);
    }

};    

function findChildList(frameId,framesList){
    for ( var i = 0 ; i< framesList.length ; i ++){
    if(framesList[i].parentFrameId == frameId){
        childList.push(framesList[i]);
    }
    }    

    return childList;
}
function drawOnPage(frameDetail){ 

    if(frameDetail.frameId == 0){
    var content = document.getElementById("tabs").innerHTML; /*this returns undefined*/
    content = ( content + "<p>Frame ID : HaHA</p>"); 
    document.getElementById("frame0").innerHTML = content;

    }
}


//listener for the messages send from popup page and content scripts
$(document).ready(function() {

     if(request.words == "tabStructure"){
        chrome.windows.getCurrent(function(win) { 
        chrome.tabs.query( {'windowId': win.id, 'active': true}, function(tabArray){ 
            if (tabArray) { 
            tabId2 = tabArray[0].id;
            chrome.webNavigation.getAllFrames({tabId: tabArray[0].id},getAllFramesCallback);
            } 
        }); 
        });
        chrome.windows.create({url:"tabStructure.html", type:'popup', height:450, width:600, left:1500, focused:true});
    }
    });

});

0 个答案:

没有答案