首先,我想承认我在JavaScript方面并不擅长。实际上,它使我无休止,并且似乎无法绕开它,仍然给它一个机会。所以我有这个“ 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行给我一个错误,原因为空。我做了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,然后将其返回值(即undefined)设置为myNewTab.onload。因此,该行应为myNewTab.onload = cont;

关于javascript - 为什么我无法获取JavaScript中元素的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28425829/

10-12 00:14
查看更多