JavaScript入门

JS 可以写到哪里


位置一,</head/> 前面 使用 <script></script> 标签

引入 js 文件,使得 js 文件和 html 文件分离,一个 js 文件可以被多个 html 使用。

位置二,内嵌 js 不能和 引入 js 文件糅杂使用

1
<script src="static/js1/a.js"></script>

注意:必须写双标签,否则引入 js 无效,而且如果引入了 js,就会去加载引入的js,在引入 js 代码的标签中写代码无效。

2019-08-12 at 9.15 P

上述代码的含义:当 html 解析到 src 会重新发送请求,如上图。

位置三

1
<a href="javascript:alert('位置三')">点击</a>

打印


1
2
3
4
5
console.log(a);
console.info(a);
console.warn(a);
console.error(a);
console.debug(a);

2019-08-12 at 9.33 P

运算符


在逻辑运算符中,0、””、falseNaNundefinednull 表示为 false,其他数据都为 true

一些特殊的 js 代码


1
2
3
4
5
6
7
8
9
10
11
1 && console.log(1);
/**
* 等价于 if(1){console.log(1);}
*/

function fun(x) {
x = x || 1; // 如果你传入了假值,x 的默认值就为 1
console.log(x);
}

fun(null);

BOM

—

BOM 是 browser object model 的缩写,简称浏览器对象模型

location

1
2
// 跳转其他界面
location.href = "www.baidu.com"

弹窗

1
2
3
4
5
6
// 弹框
alert(1);

// 对话框
var result = prompt('你的贵姓?');
console.log(result);

2019-08-13 at 10.30 A

1
2
var result = confirm('你幸福吗?');
console.log(result);

2019-08-13 at 10.31 A

定时器:

1
2
3
4
5
6
7
8
9
10
11
// 定时器

// 2 秒后执行下面这个匿名函数
setTimeout(function () {
console.log(1)
}, 2000)

// 每隔 2 秒执行下面这个匿名函数,
setInterval(function () {
alert(3);
}, 2000)

5庙后跳转页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 5 秒后跳转页面
<span id="s">5</span>秒后跳转

<script>
setInterval(function () {
var spanEle = document.getElementById('s');
// 改变 span 元素中间的值
var val = spanEle.innerHTML;
if (val == 1) {
location.href = 'www.baidu.com';
}
spanEle.innerHTML = val - 1;
}, 1000)

// 清除定时器
clearTimeout(time);
clearInterval(timer);
</script>

关于计时器

jQuery 完成表单校验


jquery 库,jquery-validation/jquery.validate.min.js
自定义提示语句

messages_cn.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
jQuery.extend(jQuery.validator.messages, {
required: "必选字段",
remote: "请修正该字段",
email: "请输入正确格式的电子邮件",
url: "请输入合法的网址",
date: "请输入合法的日期",
dateISO: "请输入合法的日期 (ISO).",
number: "请输入合法的数字",
digits: "只能输入整数",
creditcard: "请输入合法的信用卡号",
equalTo: "请再次输入相同的值",
accept: "请输入拥有合法后缀名的字符串",
maxlength: jQuery.validator.format("请输入一个长度最多是 {0} 的字符串"),
minlength: jQuery.validator.format("请输入一个长度最少是 {0} 的字符串"),
rangelength: jQuery.validator.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"),
range: jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
max: jQuery.validator.format("请输入一个最大为 {0} 的值"),
min: jQuery.validator.format("请输入一个最小为 {0} 的值")
});

引入和使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<script type="text/javascript" src="/js/plugins/jquery-validation/jquery.validate.min.js"></script>
<script type="text/javascript" src="/js/plugins/jquery-validation/message/messages_cn.js"></script>

// 数据校验
$('#editForm').validate({
//验证逻辑
rules: {
//单个验证
//name:"required"
//多个验证
name: {
required: true,
minlength: 1/*,
remote:{
url:"/employee/checkname.do",
type:"post",
data:{ //发起请求参数
name:function(){
return $("#name").val();
},
id:function () {
//需要id:原因:编辑时,用户已经存在了
//需要使用id来区分编辑还是添加操作
return $("#id").val();
}
}
}*/
},
password: {
required: true,
rangelength: [4, 6]
},
repassword: {
equalTo: "#password"
},
age: {
digits: true,
range: [18, 65]
},
email: {
required: "邮箱必填",
email: "邮箱格式不正确"
}
},
//验证失败后提示
messages: {
//可以不写,使用默认的
name: {
required: "用户必填",
minlength: "用户名长度必须大于5个"
}
},
submitHandler:function(form) {
$('#editForm').ajaxForm(function (data) {
if (data.success) {
$.messager.confirm("温馨提示", "保存成功", function () {
window.location.href = "/employee/list"
})
} else {
$.messager.alert("温馨提示", data.message)
}
})
}
})

上述校验只有对原生的和同步请求有效。对异步请求和插件提交请求无效。
没有明确调用 submit() 或者按钮不是 submit 按钮,默认都无效

1
2
3
4
5
6
7
8
9
10
11
submitHandler:function(form) {
$('#editForm').ajaxForm(function (data) {
if (data.success) {
$.messager.confirm("温馨提示", "保存成功", function () {
window.location.href = "/employee/list"
})
} else {
$.messager.alert("温馨提示", data.message)
}
})
}

覆写 submitHandler 才可以进行校验并自己提交,必须自己提交因为,submitHandler 已经覆盖了原来的方法,无法自动提交了。

遍历选出要删除的数据的 id

1
2
3
4
5
6
7
var checks = $('.selectOne:checked');
// 注意 checkBox 是 dom 元素,要封装成 jQuery 元素
$.each(checks, function(index, checkbox)) {
$(checkBox).data('id');
})

<td><input type="checkbox" class="cb selectOne" data-id="${entity.id}"></td>
文章作者: Ammar
文章链接: http://lizhaoloveit.cn/2016/08/05/JavaScript-%E5%85%A5%E9%97%A8/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Ammar's Blog
打赏
  • 微信
  • 支付宝

评论