您的位置:首页 > Web前端 > JavaScript

js 相关笔记

2016-07-22 13:16 337 查看
1.

var gridFrame = document.getElementsByTagName('iframe')[0];//获取当前frame
gridFrame.contentWindow//获取iframe的当前window,然后可以使用其中的对象了
gridFrame.src//iframe当前地址


2.JSON.stringify(obj);将obj解析成字符串;JSON.parse(string);将字符串解析成对象

  注意:有的ie不支持JSON.parse('<%=objectList%>'),可用eval('('+'<%=objectList%>'+')');代替

3.display:inline; 内联元素,简单来说就是在同一行显示。

  display:block; 块级元素,简单来说就是就是有换行,会换到第二行。

  display:inline-block; 就是在同一行内的块级元素。

4.join(","),用逗号连接数组中元素,并返回一个字符串;

  reverse()颠倒数组的顺序,并返回一个新的数组,在原数组基础上操作!!!

  sort();对数组元素排序;如果要数组按照指定规则排序,要传递一个比较函数作为参数,

  push(),向数组尾部追加一个或多个元素,并返回最新长度;

  pop(),删除数组最后一个元素,并返回删除的元素;

  unshift(),shift()和push(),pop()类似只不过前者是在数组的头部进行元素的插入或删除
5.清除掉select下的所有option

第一种:

<select name=mySelect>

<option value=1>1</option>

<option value=2>2</option>

</select>

<script language="javascript">

var theSelect=document.all.mySelect;

for(var i=theSelect.options.length-1;i>=0;i--)

theSelect.options.remove(i);

</script>


第二种:

document.getElementById("selectIDname").innerHTML = "";


6.input标签设置为disable后是不能进行写操作的,所以在后台是接收不到值的,

建议用readonly替代

7.js验证yy-MM-dd mm:HH:ss时间格式

if (!(/^\d{4}-(0\d|1[0-2])-([0-2]\d|3[01])( ([01]\d|2[0-3])\:[0-5]\d\:[0-5]\d)$/).test(startTime)) {
alert("时间格式不正确!");
return false;
}
8.JSTL中实现if else:
<c:choose>
<c:when test="">
when标签的输出
</c:when>
<c:otherwise>
otherwise标签的输出
</c:otherwise>
</c:choose>
9.JSTL中遍历数组对象可用foreach,遍历字符串则用forTokens,用delims=","隔开

10. <c:set var="totalCount" value="${totalCount+1}" scope="application"></c:set>

    <c:set var="myCount" value="${myCount+1}" scope="session"></c:set>

11. js 对数组去重

function unique(arr) {
var result = [], hash = {};
for (var i = 0, elem; (elem = arr[i]) != null; i++) {
if (!hash[elem]) {
result.push(elem);
hash[elem] = true;
}
}
return result;
}
12.正则表达式中/i,/g,/ig,/gi,/m的区别和含义

/i (忽略大小写)

/g (全文查找出现的所有匹配字符)

/m (多行查找)

/gi(全文查找、忽略大小写)

/ig(全文查找、忽略大小写)
13.去除空格

/**
* @function (去掉前后空格)
*/
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
/**
* @function (去掉左边空格)
*/
String.prototype.LTrim = function()
{
return this.replace(/(^\s*)/g, "");
}
/**
* @function (去掉右边空格)
*/
String.prototype.RTrim = function()
{
return this.replace(/(\s*$)/g, "");
}

14. 过滤非法字符

var reg = /^(?:[\u4e00-\u9fa5]*[\w]*)+$/;
if(!reg.test(num)||num.indexOf(" ")>=0){
alert("试卷代码不能包含特殊字符及空格!");
return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  js