js的一些方法(获取当前页面路径,元素失去鼠标焦点,键盘点击,键盘码,对字符串的split和replace操作)

设置或获取对象指定的“文件名”或路径。

<script>
alert(window.location.pathname)
</script>

设置或获取整个 URL 为字符串。
<script>
alert(window.location.href); 
</script>

设置或获取 href 属性中跟在问号后面的部分。
<script>
alert(window.location.search)
</script>

//当元素失去焦点时发生 blur 事件。

//blur() 函数触发 blur 事件,或者如果设置了 function 参数,该函数也可规定当发生 blur 事件时执行的代码。

//jq库方法,获取id为text标签

$("#text").

on("blur", function () {

makeCode();

}).

//监听键盘点击事件,e.keyCode==13,是enter键,也就是当按下enter,执行

on("keydown", function (e) {

if (e.keyCode == 13) {

makeCode();

}

});

以这个为例子说字符串操作

var x= http://xxxxxx/weboffice/test5.html?flag=/upload/file/201808/1e307f40-1bfc-44c3-9385-4e88964d5261.docx

x.split("?")[1].split("=")[1];意思是以?为点切割,取第二部分,再以=号切割,取第二部分

x.replace(new RegExp("/","g"),"");

replace是替换字符串,但是只替换第一个,js暂时没有全部替换,于是写一个正则表达式RegExp("/","g"),g 修饰符用于执行全局匹配

猜你喜欢

转载自blog.csdn.net/weixin_42727578/article/details/81251705
今日推荐