關于綁定事件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js練習</title>
</head>
<body>
<input type="button" value="彈窗" onclick="javascript:alert('看看')">
<input type="button" value="彈窗" id="button">
<script type="text/javascript">
var xp=document.getElementById('button');
?xp.onclick=alert('測試');
</script>
</body>
</html>
為什么我一刷新頁面就自動彈出測試??難道不應該是我點擊第二個按鈕才出現(xiàn)嗎??
2019-08-08
<html>
<head>
<meta charset="UTF-8">
<title>js練習</title>
</head>
<script type="text/javascript">
?function aa(){
??? ?var xp=document.getElementById('button');
?xp.onclick=alert('測試');
?}
</script>
<body>
<input type="button" value="彈窗" onclick="javascript:alert('看看')">
<input type="button" value="彈窗" onclick="aa()" id="button">
</body>
</html>
2016-10-27
alert需要在
function xx(){
? ? 這里
} ? ?
? 才會點擊觸發(fā),否則是會自動觸發(fā)的。
2016-09-21
好吧,我知道了。。alert本來就是個函數(shù),加個括號就自動調用了。。