JavaScript 知识点总结

1.获取iframe父窗口的dom节点

parent.$("#id");
$('#父窗口中元素的id', parent.document).事件(); 

window.parent.document.getElementById("父窗口中元素的id").事件(); //js

2.获取iframe子窗口的dom节点

parent.$("#iframe的id").contents().find("#iframe中控件的id").事件();
parent.$("#iframe中控件的id",document.frames("iframe的name").document).事情();

window.frames["iframe中的name值"].document.getElementById("iframe中控件的id").事件();//js

3.将JSON对象转化为JSON字符

var last=JSON.stringify(obj);
var last=obj.toJSONString();

4.由JSON字符串转换为JSON对象

var obj = JSON.parse(str);
var obj = str.parseJSON();

5.数组转字符串

a.join("-");

6.字符串转数组

a.split(",");

7.js动态加载文件

document.write("<script type='text/javascript' src='" + new Date().getTime() +"'><\/script>"); //需转义

8.字符串转对象

var a = "['dafadsf']";
var b = eval('('+a+')')[0];//"['dafadsf']" ---> 'dafadsf'

9.替换replace

str.replace(new RegExp("<","g"),'<').replace(new RegExp(">","g"),'>');

10.删除List对象中的某个对象

angular.forEach($scope.files,function(n,i){
    if(file.fileid == n.fileid){
        $scope.files.splice(i,1);
    }
});

11.合并两个json对象

var first = [{"a":"b"}];
var second = [{"a":"c","e":"sd"}];
$.merge(first,second);//[{"a":"b"},{"a":"c","e":"sd"}]

12.判断select是否选中

//1.判断option是否被选中
$("#id").is(":checked");//为false时是未被选中的,为true时是被选中

$("#id").attr('checked')==undefined//为false时是未被选中的,为true时是被选中

//2.获取select选中的值
$("#mySelect option:selected").text();

$("#mySelect").find('option:selected').text();

$("#mySelect").val();

//3.获取select选中的索引
$("#mySelect").get(0).selectedindex;

13.截取特殊字符串最后一位

var _href = "/a/b/c";
var _l = _href.split("/");
_l.splice(_l.length-1,1);
var _s = _l.join('/');

14.判断dom是否显隐

$div.is(':hidden');
$div.is(':visible');
$div.css('display') == 'none';

15.阻止浏览器后退

if (window.history && window.history.pushState) {
    $(window).on('popstate', function () {
        window.history.pushState('forward', null, '#');
        window.history.forward(1);
    });
}
window.history.pushState('forward', null, '#'); //在IE中必须得有这两行
window.history.forward(1);

补充:

1).滚动条置顶的代码

$(document).scrollTop(0);

2).后退的代码

window.history.back();
history.go(-1);

16.js删除iframe

parent.$("#fileImportIO").attr("src","about:blank");
document.getElementById("fileImportIO").parentNode.removeChild(document.getElementById("fileImportIO"))

17.将字符串"abcdef"转换成"ab,cd,ef"的方法

var reg = new RegExp("(\\w{2})(?=.)",'\g');
var str = "abcded";
var a = str.replace(reg,'$1,');

18.好用的可以滚动的标签(只支持ie)

<marquee></marquee>

19.清空file上传的文件

$file[0].select();
$file[0].outerHTML = $file[0].outerHTML;//已测
//网上查的两种方式
$file[0].select(); 
document.selection.clear();//1

$file[0].outerHTML=obj.outerHTML;//2

20.字符串截取(截取第三个‘/’之后的字符串)

var s = "http://localhost/services/admin/supplier/self/846050";
console.log(s.split("/").slice(3).join('/'))

21.格式化list数据的代码

JSON.stringify(str,null,'\t');
JSON.stringify(str,null,4); //使用四个空格缩进