为什么我无法在javascript中获取元素的值
How come i cannot get the value of an element in javascript
首先,我想承认我在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;
。
相关文章:
- 如何在jQuery中获取元素的形式
- 通过id和class属性获取元素
- Javascript:Can't使用getElementById获取元素
- 使用jquery.html()获取元素本身的html
- 如何从iframe内部获取元素
- Javascript函数获取元素内容
- 使用模板后,使用Angular获取元素属性值
- 获取元素类名的部分
- 从JSON.parse获取元素
- 获取元素内部的缩写
- 在不引用文档的情况下使用AngularJS获取元素
- 如何获取元素's的title属性
- 使用Javascript,获取元素的方法是什么,基于打开和关闭标记之间的文本
- Javascript获取元素背景图像,但消除了“;url()”;
- Javascript:按类获取元素,但跳过某个类
- 当display:none时,getElementById()无法获取元素
- 我的ID获取元素不起作用
- 如何获取元素:使用javascript和CORS悬停背景
- 能够获取元素长度,但不能获取内部 HTML
- 如何使用jQuery在模板kendo UI中获取元素类