關(guān)于綁定事件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js練習(xí)</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('測(cè)試');
</script>
</body>
</html>
為什么我一刷新頁(yè)面就自動(dòng)彈出測(cè)試??難道不應(yīng)該是我點(diǎn)擊第二個(gè)按鈕才出現(xiàn)嗎??
2019-08-08
<html>
<head>
<meta charset="UTF-8">
<title>js練習(xí)</title>
</head>
<script type="text/javascript">
?function aa(){
??? ?var xp=document.getElementById('button');
?xp.onclick=alert('測(cè)試');
?}
</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(){
? ? 這里
} ? ?
? 才會(huì)點(diǎn)擊觸發(fā),否則是會(huì)自動(dòng)觸發(fā)的。
2016-09-21
好吧,我知道了。。alert本來(lái)就是個(gè)函數(shù),加個(gè)括號(hào)就自動(dòng)調(diào)用了。。