常用小方法

1.字符串替换:replace,示例如下:

<script type="text/javascript">

var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/, "W3School"))

</script>
输出:Visit W3School!
2.动态获取地址栏IP地址:

  var curWwwPath = window.document.location.href;           //获取地址栏当前地址
  var pathName = window.document.location.pathname;      //获取端口号后面至html问号之前的地址
  var pos = curWwwPath.indexOf(pathName);                      //获取pathName首次在当前地址中出现的位置
  var localhostPaht=curWwwPath.substring(0,pos);             //截取字符串,截取的字符串为从第1个开始截止到端口号结束位置的字符串
  var index = localhostPaht.lastIndexOf(":");                         //返回'':''最后一次出现的位置
  var ip = localhostPaht.substring(0,index);                          //截取字符串,从第一个位置开始截取,结束位置为最后一次出现“:”的位置

示例如下:

http://11.222.333.44:8080/LYProject/travelpublic/index/330800000000.html?stbId=123&wasuId=null&returnUrl=null

var curWwwPath = window.document.location.href;     

curWwwPath 值为:http://11.222.333.44:8080/LYProject/travelpublic/index/330800000000.html?stbId=123&wasuId=null&returnUrl=null

var pathName = window.document.location.pathname;    pathName 值为:/LYProject/travelpublic/index/330800000000.html

var pos = curWwwPath.indexOf(pathName);                     pos值为:25

var localhostPaht=curWwwPath.substring(0,pos);             localhostPaht值为:http://11.222.333.44:8080

var index = localhostPaht.lastIndexOf(":"); 

var ip = localhostPaht.substring(0,index);                         ip值为:http://11.222.333.44

3.css样式

transform:元素移动

transition:元素过渡

4.禁用打印按钮,并使按钮变灰色

 $("#printTable").attr("disabled","true");
$("#printTable").attr("disabled",true);
$("#printTable").attr("disabled","disabled");
$("#printTable").addClass('locked');
.locked{
background-color: rgba(206, 214, 224, 0.863);
}

5.js去掉前后两边的双引号

d = d.replace("\"","").replace("\"","");
如果不确定有多少个双引号
d = d.replace(/\"/g, "");

6.get请求、post请求 传值方式总结

psot请求传值

var params={};
var businessdatas={};
businessdatas.aac147=cardInfo.cardNo;
businessdatas.aae140=type;
params.func='EP020001';
params.businessdatas=businessdatas;
"param="+encodeURI(JSON.stringify(params))
get请求传值
params=encodeURIComponent("cardId="+cardInfo.cardNo+"&sex="+sex+"&name="+cardInfo.name+"&birthday="+formatedDate);

7.JSON.parse 

json.parse()通常用于和服务端数据交换时, 将服务端传来的字符串转换为js对象

8.html页面拼接表格

在html页面当中拼接表格,并拼接一个onclick()方法

var listLength = list.length;
var html = '<tr><th width="10%">序号</th><th class="alginLeft">事项名称</th><th width="15%">操作</th></tr>';
for(var i = 0; i < listLength; i++) {
	var j=i+1;
	html += '<tr>';
	html += '<td>' + j + '</td>';
	html += '<td>' + list[i].title + '</td>';
	html += '<td><button onclick="gotoUrl(\''+encodeURIComponent(list[i].url)+'\')"  class="button s-button orange-button">办理</button></td>';
	html += '</tr>';
}
$("#tableData").html(html);	

  

 

 
posted @ 2018-09-14 11:36  骑过羊  阅读(257)  评论(0)    收藏  举报