document.getElemenyById 不在一个文件中工作,但在其他文件中工作
找不到...... ;
该函数正在我的一个文件中运行,但在其他文件中不起作用,我已经检查了所有名称等,但在第一个文件
<script type="text/javascript" >
function fillDataInCommand()
{
// var abc = document.myApp.getDataForCommand();
var abc ="heelo syed ammar hassan is here";
document.getElementById("commandtextarea").value = abc;
}
</script>
<form id="form1" name="form1" method="post" action="">
<label>
<textarea name="commandtextarea" style="background-color:#CCCCCC" cols="80" rows="20" wrap="off" id="commandtextarea"></textarea>
</label>
</form>
和其他文件中
document.write ("<div align='center'><textarea name='commandtextarea' style='background-color:#EFEFEF' cols ='70' rows='20' rap='off' id='commandtextarea' readonly='readonly'>abc</textarea></div>");
alert(document.getElementById("commandtextarea").value.toString());
this function is running in one of my files but not working in other one, i have checked all the names etc but couldn't find.....
in first file
<script type="text/javascript" >
function fillDataInCommand()
{
// var abc = document.myApp.getDataForCommand();
var abc ="heelo syed ammar hassan is here";
document.getElementById("commandtextarea").value = abc;
}
</script>
<form id="form1" name="form1" method="post" action="">
<label>
<textarea name="commandtextarea" style="background-color:#CCCCCC" cols="80" rows="20" wrap="off" id="commandtextarea"></textarea>
</label>
</form>
and in other one;
document.write ("<div align='center'><textarea name='commandtextarea' style='background-color:#EFEFEF' cols ='70' rows='20' rap='off' id='commandtextarea' readonly='readonly'>abc</textarea></div>");
alert(document.getElementById("commandtextarea").value.toString());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为 document.write 函数。之后,文档中将不再有文本框,因此 document.getElementById 会失败。
document.write 语句必须在页面完成加载之前运行。这意味着它们必须位于页面主体中或从页面主体调用的函数中。
It is because of the document.write function. After that you won't have a textbox in the document and so document.getElementById fails.
document.write statements must be run before the page finishes loading. This means that they must be either in the body of the page or in functions called from the body of the page.
您在第二个文件中使用
document.getElementByName
。您的意思是使用document.getElementById
。编辑:至少你是。
You're using
document.getElementByName
in the second file. You mean to be usingdocument.getElementById
.EDIT: you were, at least.
单独尝试alert(document.getElementByName("commandtextarea").value。
Try alert(document.getElementByName("commandtextarea").value alone.