Skip to content

Instantly share code, notes, and snippets.

@neekey
Created March 26, 2013 07:59
Show Gist options
  • Save neekey/5243780 to your computer and use it in GitHub Desktop.
Save neekey/5243780 to your computer and use it in GitHub Desktop.
让HTML和Javascript保持分离
<!DOCTYPE html>
<html>
<head>
<title>title</title>
</head>
<body>
<form>
<input type="text" name="name" id="my-name" placeholder="输入您的姓名" />
<button type="submit" id="submit-form">提交</button>
</form>
<script>
var myNameIpt = document.getElementById( 'my-name' );
var submitBtn = document.getElementById( 'submit-form' );
submitBtn.onclick = checkForm;
function checkForm(){
var name = myNameIpt.value;
if( !name ){
alert( '姓名不能为空');
myNameIpt.focus();
return false;
}
}
</script>
</body>
<html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment