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

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

本文关键字:获取 元素 javascript 为什么      更新时间:2023-09-26

首先,我想承认我在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行给了我一个错误原因null。我制作了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;