Live2D

jquery/js知识点收藏

1】关于页面跳转

"window.location.href"、"location.href"是本页面跳转

"parent.location.href"是上一层页面跳转

"top.location.href"是最外层的页面跳转
View Code

,在mvc项目中实践得到 parent.location.href='/'可以从任何以页面跳转到登录页面,但是会出现本页面镶嵌登录页面的情况,而top.location.href='/'则可以避免这种情况的发生

2】某个字符串是否包含某个字符串

方法一: indexOf()   (推荐)

var str = "123";
console.log(str.indexOf("3") != -1 );  // true
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。如果要检索的字符串值没有出现,则该方法返回 -1。

 

方法二: search() 

var str = "123";
console.log(str.search("3") != -1 );  // true
search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。如果没有找到任何匹配的子串,则返回 -1。

 

方法三:match()

var str = "123";
var reg = RegExp(/3/);
if(str.match(reg)){
    // 包含        
}
match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
View Code

 3】js替换字符串

var str = 'abcadeacf';
var str1 = str.replace('a', 'o');
alert(str1);  
 
// 打印结果: obcadeacf
 
var str2 = str.replace(/a/g, 'o');
alert(str2); 
View Code

 3】js/jquery操作css

$("#show").css("display", "inline ");
document.getElementById("show").style.display = "block";
View Code

 4】js获取随机数

// 结果为0-1间的一个随机数(包括0,不包括1) 
var randomNum1 = Math.random();
//console.log(randomNum1);

// 函数结果为入参的整数部分。 
var randomNum2 = Math.floor(randomNum1);
//console.log(randomNum2);

// 函数结果为入参四舍五入后的整数。
var randomNum3 = Math.round(randomNum1);
//console.log(randomNum3);

// Math.ceil(n); 返回大于等于n的最小整数。
var randomNum4 = Math.ceil(Math.random() * 10); // 主要获取1到10的随机整数,取0的几率极小。
//console.log(randomNum4);

// Math.round(n); 返回n四舍五入后整数的值。
var randomNum5 = Math.round(Math.random()); // 可均衡获取0到1的随机整数。
//console.log(randomNum5);
var randomNum6 = Math.round(Math.random() * 10); // 可基本均衡获取0到10的随机整数,其中获取最小值0和最大值10的几率少一半。
//console.log(randomNum6);

// Math.floor(n); 返回小于等于n的最大整数。
var randomNum7 = Math.floor(Math.random() * 10); // 可均衡获取0到9的随机整数。
//console.log(randomNum7);

// 获取最小值到最大值之前的整数随机数
function GetRandomNum(Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    return(Min + Math.round(Rand * Range));
}
var num = GetRandomNum(1, 10);
//console.log(num);

//获取n位随机数,随机来源chars
var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
function generateMixed(n) {
    var res = "";
    for(var i = 0; i < n; i++) {
        var id = Math.ceil(Math.random() * 35);
        res += chars[id];
    }
    return res;
}
var num1 = generateMixed(6);
//console.log(num1);
View Code

 5】ajax和post

$.ajax({
type: 'POST',
url: "@Url.Action("视图", "控制器")",
data: { Id: id},
async: false,
dataType: 'json',
success: function (result) {
$.messager.alert("提示", result, "info");
}, error: function (result){ 
location.href = "@Url.Action("视图", "控制器")?DepId=" + depid; 
}
});



$.post("@Url.Action("视图", "控制器")",{id:id},function(data){})
View Code

 

 6】json和字符的相互转换

var string = JSON.stringify(jsonx);//json转换字符串
var jsonx = JSON.parse(string);//字符串转化json
View Code

 

 7】js获取下拉框的text index value

//$('#testSelect option:selected').text();//选中的文本

                                    //$('#testSelect option:selected').val();//选中的值

                                    //$("#testSelect ").get(0).selectedIndex;//索引
View Code

 

 8】Ajax格式

$.ajax({
    url:"http://www.test.com",    //请求的url地址
    dataType:"json",   //返回格式为json
    async:true,//请求是否异步,默认为异步,这也是ajax重要特性
    data:{"id":"1","name":"名字"},    //参数值
    type:"GET",   //请求方式
    beforeSend:function(){
        //请求前的处理
    },
    success:function(req){
        //请求成功时处理
    },
    complete:function(){
        //请求完成的处理
    },
    error:function(){
        //请求出错处理
    }
});
 
View Code

 

 9】This以及删除节点的使用格式

html
<a  onclick='DelThis(this)'    name='nameDel'>删除</a>

js代码
 function DelThis(obj) {
            var v1 = $(obj).attr("name");
            var o = document.getElementById("divContent");//获取父节点
            var a = document.getElementById(v1);//获取需要删除的子节点
            o.removeChild(a)//从父节点o上面移除子节点a}
        }
View Code

 

 10】js获取radio选中的值

html
<input type="radio" name="sex" value="1" checked="checked" ><input type="radio" name="sex" value="2" ><input type="radio" name="sex" value="3"  >保密

js
  var radio=document.getElementsByName("sex");

              var selectvalue="";   //  selectvalue为radio中选中的值

             for(var i=0;i<radio.length;i++){

                    if(radio[i].checked==true) {

                             selectvalue=radio[i].value;

                             break;

                   }

            }
View Code

 

 10】子页面使用父页面的js方法

1父页面js方法
function get(ts){ alert(a);}

2子页面调用父页面的js方法
 parent.get("调用");

 parent.父页面的js方法();


【目前在iframe中测试完美运行】
View Code

 

 11】js获取option中自定义属性

html:

   <select id="workbook" >
   <option value="-1" tableid="2222">--请选择工作--</option>
      </select>


js:
    var obj = document.getElementById("workbook");
    var index=obj.selectedIndex;
    var a=obj.options[index].getAttribute("tableid");
View Code

 

posted @ 2018-09-17 14:32  楚景然  阅读(199)  评论(0编辑  收藏  举报