无论是前端开发还是后台设计 很多时候开发人员都需要获取当前或目标网址的相关信息 这个已有现成的内置对象属性可以直接调用了(下面是获取当前页面的参考代码)
复制代码 代码如下: <script type=text/javascript>thisURL = document URLthisHREF = document location hrefthisSLoc = self location hrefthisDLoc = document locationstrwrite = " thisURL: [" + thisURL + "]<br>" strwrite += " thisHREF: [" + thisHREF + "]<br>" strwrite += " thisSLoc: [" + thisSLoc + "]<br>" strwrite += " thisDLoc: [" + thisDLoc + "]<br>" document write( strwrite )</script>但有时候我们需要改变目前网址的参数/参数值 这时候大家会如何处理呢?一般应该都会先通过获取所有参数的信息 然后再根据实际需求来对某一参数进行修改 没错!根据这一原理 今天就给大家分享一下个人在开发中封装的功能函数 //=============== 改变网址的参数值 ================
复制代码 代码如下: function ChangeURLParm(Turl Parm PValue ClearParm){ //Turl: 网址 //Parm 参数 //PValue 参数值 //ClearParm: 要清除的参数 var URL Parms ParmsArr IsExistvar NewURL = Turl//window location href IsExist = falsewith(Turl){ if(indexOf( ? )>){ URL = substr( indexOf( ? ))//不包含参数 Parms = substr(indexOf( ? )+ length)//参数 } else{ URL = TurlParms = } } if (Parms!= ){ var iParmsArr = Parms split("&")for(i= i<=ParmsArr length i++){ if (String(Parm) toUpperCase()==String(ParmsArr[i] split("=")[ ]) toUpperCase()){//原来有参数Parm则改变其值 ParmsArr[i] = Parm + "=" + PValueIsExist = trueif (String(ClearParm) ==""){ break} } else if ( (String(ClearParm)!="") &&(String(ClearParm) toUpperCase()==String(ParmsArr[i] split("=")[ ])) toUpperCase() ){//去掉参数ClearParm的值 ParmsArr[i] = ClearParm + "="} } for(i= i<=ParmsArr length i++){ if(i== ){ Parms = ParmsArr[i]} else{ Parms = Parms + "&" + ParmsArr[i]} } NewURL = URL + "?" + Parmsif (!IsExist){ NewURL = NewURL + "&" + Parm + "=" + PValue} } else{ NewURL = URL + "?" + Parm + "=" + PValue} return NewURL} lishixinzhi/Article/program/Java/JSP/201311/20336<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<title>RunJS 演示代码</title>
<style>
.* {
padding: 0
margin: 0
}
a {
text-decoration: none
color: black
}
a:hover {
color: red
}
.wrap {
width: 900px
height: 300px
overflow-y: auto
margin: auto
}
table {
width: 800px
border: 1px solid black
border-collapse: collapse
}
th {
background-color: orange
}
td,th {
border: 1px solid black
text-align: center
}
table input[type=text] {
width: 100px
}
</style>
<script>
var yugi = {
col: 6,
addRow: function(table) {
var vals = [a.value, b.value, c.value, d.value, e.value, "<a href='###' onclick='yugi.modify(table,this)'>修改</a>&nbsp<a href='###' onclick='yugi.del(table,this)'>删除</a>"]
var tr = table.insertRow(table.tBodies[0].rows.length)
for (var i = 0 i < yugi.col i++) {
var td = tr.insertCell(tr.cells.length)
td.innerHTML = vals[i]
}
},
modify: function(table, row) {
var r = row.parentElement.parentElement,
c = r.cells
if (/.*修改.*/g.test(row.innerHTML)) {
for (var i = 0 i < c.length - 1 i++) {
var ci = c[i]
var txt = document.createElement("input")
txt.type = "text"
txt.value = ci.innerHTML
ci.innerHTML = ""
ci.appendChild(txt)
}
row.innerHTML = "保存"
} else {
for (var i = 0 i < c.length - 1 i++) {
var ci = c[i]
ci.innerHTML = ci.children[0].value
}
row.innerHTML = "修改"
}
},
del: function(table, row) {
var ind = row.parentElement.parentElement.rowIndex
table.tBodies[0].deleteRow(ind)
}
}
</script>
</head>
<body>
<fieldset class="wrap">
<legend>学生信息列表</legend>
<div>
<input type="text" id="a" />
<input type="text" id="b" />
<input type="text" id="c" />
<input type="text" id="d" />
<input type="text" id="e" />
<input type="button" value="添加" onclick="yugi.addRow(table)" />
</div>
<table id="table">
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>学历</th>
<th>性别</th>
<th>地址</th>
</tr>
</table>
</fieldset>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)