当我编译以下代码时,它工作正常,但在控制台我得到错误: 未捕获的TypeError:无法读取属性'关闭'未定义的
// init your buttons var
Button one, two, three, four, five ...;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the layout above
setContentView(R.layout.activity_main);
// init your buttons
one = (Button) findViewById(R.id.button1);
two = (Button) findViewById(R.id.button2);
three = (Button) findViewById(R.id.button3);
... etc.
// set them to your implementation
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
... etc.
}
// call this function when one button is pressed
public void onClick(View view) {
// retrieves the id of clicked button
switch(view.getId()) {
case R.id.button1:
methodToSaveNumber(int);
break;
case R.id.button2:
methodToSaveNumber(int);
break;
case R.id.button3:
methodToSaveNumber(int);
break;
... etc.
}
}

<!DOCTYPE html>
<html>
<body>
<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "myWindow"</button>
<script>
var myWindow;
function openWin(){
myWindow = window.open("http://www.w3schools.com", "myWindow");
}
function closeWin() {
myWindow.close();
}
</script>
</body>
</html>
&#13;
我试图在外部调用上面的js文件,我得到同样的错误
答案 0 :(得分:4)
在点击另一个按钮之前,你必须单击Open "myWindow"
按钮,因为那是变量myWindow获得初始值的时候。
在尝试关闭窗口之前,您应该检查窗口是否已打开:
function closeWin() {
if(myWindow){
myWindow.close();
}
}
如果未设置myWindow
,则您什么也不做(因为没有什么可以关闭,对吧?)。