为什么我无法获取 javascript 中元素的值

How come i cannot get the value of an element in javascript

首先,我想承认我在javascript方面没有任何优势。事实上,它让我无休止地沮丧,我似乎无法理解它,仍然给它一个 shot.So 我有这个 "js" 文件:

var url, tab,myNewTab;
function init(){
    chrome.tabs.query({currentWindow: true, active: true},function(tabs){
       url = tabs[0].url;
       tab = tabs[0]
       tabId = tabs[0].id;
       processTab();
    });
}

function processTab(){
    chrome.tabs.update(tabId, {selected: true});
    if (url.substring(0,5) === "http:")
    {
        console.log(window.location);
        url = insert(url , 4 , "s");
    }
    myNewTab = window.open('https://alexanderproxy33.appspot.com','_newtab' );

    myNewTab.onload = cont() ;
}
function insert(str, index, value) {
    return str.substr(0, index) + value + str.substr(index);
};
function cont()
{
    console.log(myNewTab.document.getElementById("input").value)
}
init();

我想有很多事情可以做得更好,但我的问题是:console.log 行给了我一个错误原因 null.I 制作了 cont() 函数,所以它会加载它的元素,这样我就可以得到它们(正如许多显然是正确的人所建议的那样)但仍然没有。下一部分是上述网站的 html 的一部分,我认为它会触发这个:

<body>
    <div class="box">
        <h6>Basic Proxy</h1>
            <center>
                <form action="" method="get" accept-charset="utf-8" target="_blank">
                    <input name="url" type="text" class="txt" id="input" placeholder="type url here.." onfocus="this.value='';" />
                    <input type="submit" class="btn" value="Go" />
                </form>
            </center>
            <p class="footer">Instructions: Just type the URL of any web page in the input box above (
                <em>e.g. google.com</em> ) and hit Enter.</p>
            <p class="footer"></p>
    </div>
</body>

提前致谢

myNewTab.onload = cont() ; 不正确,因为您正在执行 cont,然后将其 return 值(即 undefined)设置为 myNewTab.onload。因此,该行应该是 myNewTab.onload = cont;.