正在进行的项目需要一个Table列出一些数据,每一行记录都有一些特定的信息需要保存。比如每一行都需要属性记录是否被选中,个别行还有自己的保留信息。
比较直接的想法是用数组,但是那样的话,数组和列关联起来不是很容易。后来发现原来可以直接把数据作为HTML元素的属性保存起来
下面的函数用来在一个元素中保存一个健值对。
var IS_IE = false; var IS_MOZILLA = false; if ((!document.all)&&(document.getElementById)) IS_MOZILLA = true; if ((document.all)&&(document.getElementById)) IS_IE = true; /* * set a value to the HTML element without considering the brower * @param htmlElement the html element you want to set value * @param name name of attribute * @param value value of attribute */ function setHTMLAttribute(htmlElement,name,value){ if (IS_MOZILLA) htmlElement.setAttribute(name,value); if (IS_IE) htmlElement[name] = value; }
之后就可以通过document.getElementById('***').getAttribute('***')来取得之前保存的值了。