- 爱易网页
-
JavaSript
- 一个js完整的事例
日期:2014-05-16 浏览次数:20357 次
一个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(""+newRow.id+"");'/>";
newRow.appendChild(newCol);
newCol = document.createElement('td');
newCol.width = "50%";
newCol.innerHTML = "<input type='text' id='attr_"+trinc+"_val' size='50' onFocus='assignValue(""+newRow.id+"");'/>";
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>
免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。