document.getElementById("按鈕名").onclick = function() { 執(zhí)行 } 分開寫要怎么寫呢?
document.getElementById("按鈕名").onclick = function() { 執(zhí)行 }?
?分開寫要怎么寫呢??
例如:?
document.getElementById("按鈕名").onclick = 函數(shù)名();?
function 函數(shù)名() { 執(zhí)行 }?
?這樣寫好像運(yùn)行無效……
2018-07-19
var request = null;
//查詢員工
document.getElementById('search').onclick = function(){
//發(fā)送AJAX查詢請求并處理
var request = new XMLHttpRequest();
request.open('GET','server.php?number='+document.getElementById('keyword').value);
request.send(null);
request.onreadystatechange = state_Change;
};
function state_Change(){
//此處是this
console.log(this.readyState);
if (this.readyState == 4) {
if (this.status == 200) {
document.getElementById('searchResult').innerHTML = this.responseText;
}else{
alert('發(fā)生錯誤!' + this.status);
}
}
}
2016-11-03
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>無標(biāo)題文檔</title>
<style>
#div1{width:100px; height:100px; background:red;}
</style>
<script>
window.onload=function(){
? ? document.getElementById('div1').onclick=function(){
fn1(1)
};
? ? function fn1(a){
? ? ? ? alert(a)
? ? };?
};
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
用一個匿名函數(shù)包起來??!
2016-11-01
不能加括號,加括號會默認(rèn)為window的函數(shù)調(diào)用,不加才是div1的onclick調(diào)用
2016-10-29