2016-11-24

JavaScript escape() 函数

JavaScript 全局对象

定义和用法:
escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。

语法:escape(string)

参数 描述
string 必需:要被转义或编码的字符串。

返回值:
已编码的 string 的副本。其中某些字符被替换成了十六进制的转义序列。

说明:
该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / 。其他所有的字符都会被转义序列替换。

提示:可以使用 unescape() 对 escape() 编码的字符串进行解码。

注释:ECMAScript v3 反对使用该方法,应用使用 decodeURI() 和 decodeURIComponent() 替代它。

实例:
在本例中,我们将使用 escape() 来编码字符串:

<script type="text/javascript">

document.write(escape("Visit W3School!") + "<br />")
document.write(escape("?!=()#%&"))

</script>

输出:

Visit%20W3School%21
%3F%21%3D%28%29%23%25%26
//设置cookie  
function setCookie(cookieName,cookieValue,cookieExpires,cookiePath)  
{  
    cookieValue = escape(cookieValue);//编码latin-1  
    if(cookieExpires=="")  
    {  
        var nowDate = new Date();  
        nowDate.setMonth(nowDate.getMonth()+6);  
        cookieExpires = nowDate.toGMTString();  
    }  
    if(cookiePath!="")  
    {  
        cookiePath = ";Path="+cookiePath;  
    }  
    document.cookie= cookieName+"="+cookieValue+";expires="+cookieExpires+cookiePath;  
}  
 //获取cookie  
 function getCookieValue(cookieName)  
{  
    var cookieValue = document.cookie;  
    var cookieStartAt = cookieValue.indexOf(""+cookieName+"=");  
    if(cookieStartAt==-1)  
    {  
        cookieStartAt = cookieValue.indexOf(cookieName+"=");  
    }  
    if(cookieStartAt==-1)  
    {  
        cookieValue = null;  
    }  
    else  
    {  
        cookieStartAt = cookieValue.indexOf("=",cookieStartAt)+1;  
        cookieEndAt = cookieValue.indexOf(";",cookieStartAt);  
        if(cookieEndAt==-1)  
        {  
            cookieEndAt = cookieValue.length;  
        }  
        cookieValue = unescape(cookieValue.substring(cookieStartAt,cookieEndAt));//解码latin-1  
    }  
    return cookieValue;  
} 

JavaScript indexOf() 方法

JavaScript String 对象

定义和用法:
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。

语法:
stringObject.indexOf(searchvalue,fromindex)

参数 描述
searchvalue 必需。规定需检索的字符串值。
fromindex 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的首字符开始检索。

说明:
该方法将从头到尾地检索字符串 stringObject,看它是否含有子串 searchvalue。开始检索的位置在字符串的 fromindex 处或字符串的开头(没有指定 fromindex 时)。如果找到一个 searchvalue,则返回 searchvalue 的第一次出现的位置。stringObject 中的字符位置是从 0 开始的。

注释:indexOf() 方法对大小写敏感!

注释:如果要检索的字符串值没有出现,则该方法返回 -1。

<!doctype html>  
<html>  
<head>  
<title>cookie</title>  
<meta charset="utf-8">  
<script language="<a href="http://lib.csdn.net/base/javascript" class='replace_word' title="JavaScript知识库" target='_blank' style='color:#df3434; font-weight:bold;'>JavaScript</a>" type="text/javascript">  
    //获取cookie  
     function getCookieValue(cookieName)  
    {  
        var cookieValue = document.cookie;  
        var cookieStartAt = cookieValue.indexOf(""+cookieName+"=");  
        if(cookieStartAt==-1)  
        {  
            cookieStartAt = cookieValue.indexOf(cookieName+"=");  
        }  
        if(cookieStartAt==-1)  
        {  
            cookieValue = null;  
        }  
        else  
        {  
            cookieStartAt = cookieValue.indexOf("=",cookieStartAt)+1;  
            cookieEndAt = cookieValue.indexOf(";",cookieStartAt);  
            if(cookieEndAt==-1)  
            {  
                cookieEndAt = cookieValue.length;  
            }  
            cookieValue = unescape(cookieValue.substring(cookieStartAt,cookieEndAt));//解码latin-1  
        }  
        return cookieValue;  
    }  
    //设置cookie  
    function setCookie(cookieName,cookieValue,cookieExpires,cookiePath)  
    {  
        cookieValue = escape(cookieValue);//编码latin-1  
        if(cookieExpires=="")  
        {  
            var nowDate = new Date();  
            nowDate.setMonth(nowDate.getMonth()+6);  
            cookieExpires = nowDate.toGMTString();  
        }  
        if(cookiePath!="")  
        {  
            cookiePath = ";Path="+cookiePath;  
        }  
        document.cookie= cookieName+"="+cookieValue+";expires="+cookieExpires+cookiePath;  
    }  
      
    //页面加载时间处理函数  
    function window_onload()  
    {  
        var userNameElem = document.getElementById("userName");//用户名输入框对象  
        var passwordElem = document.getElementById("password");//密码输入框对象  
        var currUserElem = document.getElementById("currUser");//复选框对象  
        var currUser = getCookieValue("currUser");  
        if(currUser!=null)  
        {  
            userNameElem.value=currUser;  
            currUserElem.checked = true;  
        }  
        if(userNameElem.value!="")  
        {  
            passwordElem.focus();//密码输入框获得焦点  
        }  
        else  
        {  
            currUserElem.focus();//用户名输入框获得焦点  
        }  
    }  
    //表单提交处理  
    function login()  
    {  
        var userNameElem = document.getElementById("userName");       
        var passwordElem = document.getElementById("password");  
        var currUserElem = document.getElementById("currUser");  
        if(userNameElem.value=="" || passwordElem.value=="")  
        {  
            alert("用户名或密码不能为空!");  
            if(userNameElem.value=="")  
            {  
                userNameElem.focus();//用户名输入框获得焦点  
            }  
            else  
            {  
                passwordElem.focus();//密码输入框获得焦点  
            }  
            return false;  
        }  
        if(currUserElem.checked)  
        {  
            setCookie("currUser",userNameElem.value,"","");//设置cookie  
        }  
        else  
        {  
            var nowDate = new Date();//当前日期  
            nowDate.setMonth(nowDate.getMonth()-2);//将cookie的过期时间设置为之前的某个日期  
            cookieExpires = nowDate.toGMTString();//过期时间的格式必须是GMT日期的格式  
            setCookie("userName","",cookieExpires,"");//删除一个cookie只要将过期时间设置为过去的一个时间即可  
        }  
        return true;  
    }  
</script>  
<style type="text/css">  
    div{  
        font-size:12px;  
    }  
</style>  
</head>  
<body onload="window_onload()">  
<div>  
<form id="loginForm" onsubmit="return login()">  
用户名:<input type="text" id="userName"><br>  
密  码:<input type="password" id="password">  
<input type="checkbox" id="currUser">记住用户名<br>  
<input type="submit" value="登录">  
</form>  
</div>  
</body>  
</html>

注意:

由于google Chrome浏览器为了安全只支持online-cookie,所以在本地测试时是没有效果的,需要上传到服务器试一下。

blur失去焦点

focus得到焦点

index([selector|element])

概述:
搜索匹配的元素,并返回相应元素的索引值,从0开始计数。
如果不给 .index() 方法传递参数,那么返回值就是这个jQuery对象集合中第一个元素相对于其同辈元素的位置。
如果参数是一组DOM元素或者jQuery对象,那么返回值就是传递的元素相对于原先集合的位置。
如果参数是一个选择器,那么返回值就是原先元素相对于选择器匹配元素中的位置。如果找不到匹配的元素,则返回-1。 具体请参考示例。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. javascript删除元素节点 IE中有这样一个方法:removeNode(),这个方法在IE下是好使的,...
    落魂灬阅读 350评论 0 1
  • 今天是入职的第二个周,周三的数据库考试26分,想想上学来没得过这么低的分数倒在第一份工作中栽了,有些想笑,有些茫然...
    长帽子的眼睛阅读 180评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • 前言 由于是用latex写的,pdf我不知道怎么传上来,转成图片放这里了,tex我也留下了,感兴趣的小伙伴如果有报...
    梁王io阅读 1,320评论 0 3
  • 拆页2 P257 任务 写一组三张便签,其中 I便签:用自己的语言简要解释什么叫从矛盾中获得洞察、怎样从矛盾中获得...
    海红米花妈阅读 196评论 0 0