<html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript"> function fun(){ var y=document.getElementById("cardnum"); var n=document.getElementById("pwd"); if(y.value == ''){ alert("Please enter the card number"); y.focus(); }
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function fun(){
var y=document.getElementById("cardnum");
var n=document.getElementById("pwd");
if(y.value == ''){
alert("Please enter the card number");
y.focus();
}
}
</script>
</head>
<body>
num:<input type="text" id="cardnum"/><br/>
pwd:<input type="text" id="pwd" onfocus="fun()"/>
</body>
</html>
上面的可以 ,你看一下 ,什么时候触发fun()这个方法的,
另一个var n=document.getElementById("pwd").value;这样取的是n的值,并不是n这个元素。jquery的我不懂。