日期:2014-05-16  浏览次数:20329 次

一个js完整的例子
1:   js 很有用的例子

<script>
var trinc = 0;
var trid;
var finalStr='';
function addRow()
{
var newRow = document.createElement('tr');
newRow.id = ("attr_"+trinc+"_tr");
var newCol = document.createElement('td');
newCol.width = "50%";
newCol.innerHTML = "<input type='text' id= 'attr_"+trinc+"' size='50' onFocus='assignValue(&quot;"+newRow.id+"&quot;);'/>";
newRow.appendChild(newCol);
newCol = document.createElement('td');
newCol.width = "50%";
newCol.innerHTML = "<input type='text' id='attr_"+trinc+"_val' size='50' onFocus='assignValue(&quot;"+newRow.id+"&quot;);'/>";
newRow.appendChild(newCol);
var tbodyElem = document.getElementById('newprops');
tbodyElem.appendChild(newRow);
trinc++;
}

function assignValue(val)
{
trid = val;
}
function deleteRow(trd, opt)
{
if(trd)
{
if(opt == 'attr' && !trd.startsWith("def_"))
{
var rowObj = document.getElementById(trd);
var bodyObj = document.getElementById("newprops");
bodyObj.removeChild(rowObj);
}
else if(trd.startsWith("def_"))
{
finalStr += document.getElementById(trd.substring(0,trd.length-3)).value+'_@_';
var rowObj = document.getElementById(trd);
var bodyObj = document.getElementById("props");
bodyObj.removeChild(rowObj);
}
}
else
{
alert("Please select an attribute !!");
return false;
}
}
function updatePage()
{
var table1 = document.getElementById("props");
var allInputs1 = table1.getElementsByTagName("input");
var table2 = document.getElementById("newprops");
var allInputs2 = table2.getElementsByTagName("input");

for(index=allInputs1.length-1;index>=0;index--)
{
if(allInputs1[index].id.startsWith("def_") && !allInputs1[index].id.endsWith("_val"))
{
attrName = document.getElementById(allInputs1[index].id).value;
attrValue = document.getElementById(allInputs1[index].id+"_val").value;
attrValueHidden = document.getElementById(allInputs1[index].id+"_hid_val").value;
if(attrName && attrName.trim() != '' && (attrValue!=attrValueHidden))
{
finalStr += (attrName+'_@@_'+attrValue+'_@_');
}
}
}
for(index=allInputs2.length-1;index>=0;index--)
{
if(allInputs2[index].id.startsWith("attr_") && !allInputs2[index].id.endsWith("_val"))
{
attrName = document.getElementById(allInputs2[index].id).value;
attrValue = document.getElementById(allInputs2[index].id+"_val").value;
if(attrName && attrName.trim() != '')
{
finalStr += (attrName+'_@@_'+attrValue+'_@_');
}
}
}
//alert(finalStr);
window.location.href = 'updateProperties?option='+document.getElementById('optionId').value+'&str='+finalStr;
}
String.prototype.trim = function (){
return this.replace(/^\\s*/, '').replace(/\\s*$/, '')}
String.prototype.startsWith = function(str)
{return (this.match('^'+str)==str)}
String.prototype.endsWith = function(str)
{return (this.match(str+'$')==str)}
</script>