Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
访问在另一个文件中创建的按钮-javascript_Javascript_Jquery - Fatal编程技术网

访问在另一个文件中创建的按钮-javascript

访问在另一个文件中创建的按钮-javascript,javascript,jquery,Javascript,Jquery,我试图在外部javascript文件中声明的函数中创建一个按钮。对于html文件(在某种意义上是“主”文件),我试图访问这个按钮并分配click侦听器。我不能这样做。请参阅下面的代码,我到目前为止 testDialogJS.js: /** * The JS here will be referenced from another file. */ function creator() { console.log("creator."); var btn = document.cr

我试图在外部javascript文件中声明的函数中创建一个按钮。对于html文件(在某种意义上是“主”文件),我试图访问这个按钮并分配click侦听器。我不能这样做。请参阅下面的代码,我到目前为止

testDialogJS.js

/**
 * The JS here will be referenced from another file.
 */

function creator() {

  console.log("creator.");
  var btn = document.createElement("BUTTON");
  var text = document.createTextNode("Click me!");
  btn.setAttribute("id", "Button");
  btn.appendChild(text);
}  
testDialog.html:

<html>

<head>
<style>
.hlight{background-color:#ffcc00; }
textarea {
    width:100%;
    height:100%;
}
</style>

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"/>
<!-- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script> -->
<script type="text/javascript" src="testDialogJS.js"></script>
<script type="text/javascript">
window.onload=function() {

  console.log("Window has been loaded.");
  creator();   //this function is elsewhere.

  var btn = document.getElementById("Button");
  if(btn == null) {
    console.log("Button is null.");
  } else {
    console.log("Button is not null.");
    btn.onclick=function() {
      console.log("Hello.");
    }
  }
}

</script>
</head>

<body>
</body>
</html>

.hlight{背景色:#ffcc00;}
文本区{
宽度:100%;
身高:100%;
}
window.onload=function(){
log(“窗口已加载”);
creator();//此函数位于其他位置。
var btn=document.getElementById(“按钮”);
如果(btn==null){
log(“按钮为空”);
}否则{
log(“按钮不为空”);
btn.onclick=函数(){
log(“你好”);
}
}
}

您没有在
文档
中添加
按钮
。因此,当您尝试使用
document.getElementById(“按钮”)

像这样在
creator()
中添加以下语句

function creator() {

  console.log("creator.");
  var btn = document.createElement("BUTTON");
  var text = document.createTextNode("Click me!");
  btn.setAttribute("id", "mybtn");
  btn.appendChild(text);
  document.body.appendChild(btn);
} 

你把那个按钮附加到文档的什么地方了?@Arvind:你说得对。我没有在文件上附加按钮。如果你能写下你的答案,我会很高兴把它标记为正确的。谢谢