您的位置:首页 > 其它

欲善其事,必利其器---页面开发基础知识整理

2016-03-25 11:22 477 查看

引言:本次只做一次javascript方面的知识梳理,决定以代码化的方式直接展示!

模块一
<script type="text/javascript">
$(function () {
//非boolean值与boolean进行相加,得出数值var $num = 3;var $bol = true;
//字符串与数字相加
var $str = "2";//alert($num+$str);//“32” 不会报错 进行字符串拼接
//alert(null.x);//TypeError: null has no properties
//alert(hellow(x));
//ReferenceError: hellow is not defined//alert($num + $bol);
//4 隐士转换 不会报错(只有调用未存在的函数或者null.XXX才会报错)
//NaN判断
var $nan = NaN;
//alert($nan === NaN);
//false (隐士转化会将 null转为0,undefined转为NaN)
//判断值是否等于NaN (由于精度的不同导致了IsNaN判断标准不同,利用IsNaN判断是否为数字不太严谨)
//alert(isNaN("x"));
//true
//alert(isNaN(undefined));
//true
//alert(isNaN({}));
//true
//替代IsNaN()判断的方式 !==
//var b = "fl";
//alert($num !== $nan);
//true
//alert(IsReallyNaN(b));
//false
//alert(IsReallyNaN(undefined));
//false
//重写IsNaN()function IsReallyNaN(x) {return x !== x;}
//对象转字符串用tostring,转数字用valueOf
var obj = {toString: function () {return "j" + $str.toString();},valueOf: function () {return 4 + $num;}}
alert(obj.toString() + "" + obj.valueOf());//j2 7
//判断值是否等于undefined (忽略了0和-0)
//function hello(x) {// if (!x) {// x = 11111;// }
// return {x:x};//}
//console.log(hello(-0));//1111
//替代逻辑运算符判断undefined的方法 typeof
function hellos(x) {
//if (typeof x==="undefined") {
// x = 11111;
//}if (x === undefined) {//不要用 x=='undefined',推荐使用===x = 11111;}//对于确定的类型判断,可以加上parseInt()更严谨if (parseInt(x) === undefined) {x = 1;}return { x: x };}
console.log(hellos());
//1111
console.log(hellos(-0));//0
});

模块二
<script type="text/javascript">
$(function () {
$('#btn_load').click(function () {
//$('.content').load('/testscript/hello', function () { });//将请求的页面填充到div中
//$.getJSON('/testscript/tojson', function (data) {//从服务器端获取json// //遍历json文件// $.each(data, function (i, value) {// //...// });//});
//$.getScript('/testscript/tojs', function () {//从服务器端获取JS// //...//});
//全局Ajax$.ajaxSetup({});
//在请求服务器之前需要做的事
$('.content').ajaxStart(function () { $(this).hide() });
//在请求服务器结束需要做的事
$('.content').ajaxStop(function () { $(this).show(); });
//序列化编码后的表单元素的值
alert($('#frm').serialize());});
//取值按钮$('#btn').click(function () {
//获取当前select标签的值
alert($('#sel').val());
//获取选中select的文本
alert($('#sel option:selected').text());
//获取当前checkbox选择状态
$('[name=chk]:checked').each(function () {
alert($(this).val());
alert($(this).attr("data-IsTrue"));});
//获取当前选中redio标签的值
alert($(':radio[name=rdo]:checked').val());
$(':radio[name=rdo]').eq(0).prop("checked", true);//默认选择第一个
//获取文本域的值alert($.trim($('#are').val()));});
//是否全选按钮$('#chkAll').click(function () {
//全选 反选
$('#chkAll').toggle(function () {$('[name=chk]').prop('checked', true);}, function () {$('[name=chk]').prop('checked', false);});
//不建议用toggle,会与jq的状态冲突
if ($(this).prop("checked")) {$('[name=chk]').prop("checked", true);}
else {$('[name=chk]').prop("checked", false);}});});
</script> </span>


知识扩展

Number()函数可以替代IsNaN()函数做是否非数字验证,但是Number()返回NaN,IsNaN()返回boolean值。

有时候做字符串非空验证可以用===“” 也可判断Length是否为0,但是前提必须做好Trim()去空格处理,空格也会占用一个字符位置。

1.判断字符串输入字符长度函数(unicode码)
var validate={
//判断字符内存中实际长度
GetStrLength:function GetStrLength(value){
var len=0;
for(var i=0; i<value.length; i++){
var a=value.charAt(i);
if(a.match(/[^\x00-\xff]/ig) !=null){
len+=2;//汉字
}else{
len+=1;//数字
}
return len;//返回字符长度
}
}
}

2.文本框中只允许限制数字输入
代码嵌入式<input type="text"  onkeyup="this.value='this.value.replace(/\D/g,'')'">  <input type="text"  onafterpaste="this.value='this.value.replace(/\D/g,'')'">
函数封装    <input type="text" data-type="print">
$('input[data-type=print]').keyup(function(){
var source=$(this).val();
source=source.replace(/[^\d]*/g,'').replace(/\b(0+)/gi,'');//过滤中文和特殊字符
$(this).val(source);
});

3.验证是否输入了正确的url(http or https)
$('#txtLinkAddress').blur(function(){
var address_str=$.trim($(this).val());
var reg=/^(http|https):\/\/([a-zA-Z\d][a-zA-Z\d-_]+\.)+[a-zA-Z\d-_][^ ]*$/;
if(reg.test(address_str)){
//友好提示.....
}
});

4.正则判断NULL OR NaN
function isNullOrNaN(value){
var flag=false;
var reg=/^([1-9]|[1-9][0-9]+)?$/;
if(reg.test(value)){
flag=true;
return flag;
}
}


(我会慢慢总结 这些以后 直接参考 不必百度谷歌 否则你一旦离开了百度谷歌 你啥都不是!)

未完待续…
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: