jQuery事件--頁面框架加載後自動執行
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.error{
color: red;
}
</style>
</head>
<body>
<form id="f1" action="6.html" method="POST">
<div><input name="n1" type="text"></div>
<div><input name="n2" type="password"></div>
<div><input name="n3" type="text"></div>
<div><input name="n4" type="text"></div>
<div><input name="n5" type="text"></div>
<input type="submit" value="提交">
<img src="..."> <!--加載某個連接圖片-->
</form>
<script src="jquery-1.12.4.js"></script>
<script>
$(function () { //最外面添加一個匿名函數
$(':submit').click(function () {
$('.error').remove();
var flag = true;
$('#f1').find('input[type="text"],input[type="password"]').each(function () {
var v = $(this).val();
if(v.length <=0){
flag = false;
var tag = document.createElement('span');
tag.className = 'error';
tag.innerHTML = '*必填選項';
$(this).after(tag)
// return false;
}
});
return flag;
})
});
</script>
</body>
</html>
代碼說明:
$(function () {
});
代碼中在最外面新加了一個匿名函數,就是爲了當有加載圖片或類似的情況時,能夠先加載頁面框架,防止圖片加載完成之前一直等待。
外面加了匿名函數,會將圖片的框架先加載出來,不會因爲其加載延遲,而影響整個頁面框架加載。