יום רביעי, 11 בספטמבר 2013

בחירת DIV והצגתו לקליינט בJAVASCRIPT שימוש בעדכון מערך

הרעיון הפשוט שישנם עשרות DIV בדף, וכל אחד הינו ייחודי בID שלו (מספרי בדוגמא שלנו), הקליינט בוחר DIV , הDIV נצבע והנתון של הID נכנס למערך.
המערך הינו בJAVASCRIPT שמתעדכן בהכנסה והוצאה של הנתונים. לחיצה על הDIV - הDIV נדלק וערכו נכנס למערך, לחיצה נוספת על הDIV - הDIV נכבה וערכו מוסר מהמערך:
 [נ.ב. הקוד משתמש בJQUERY, אז לא לשכוח להוסיף, מכיוון שהפונקציה indexOf אינה תומכת בדפדפנים ישנים]
<div id="1" onclick="takeDiv('1')" />
<div id="2" onclick="takeDiv('2')" />
<script type="text/javascript">
// Array Remove - By John Resig (MIT Licensed)
        Array.prototype.remove = function (from, to) {
            var rest = this.slice((to || from) + 1 || this.length);
            this.length = from < 0 ? this.length + from : from;
            return this.push.apply(this, rest);
       };
 function takeDiv(idDiv) {
          PrintDiv(idDiv);
          inseretToArray(idDiv);
    }
function PrintDiv(idDiv) {
         if (document.getElementById(idDiv).style.background != 'orange') {
             document.getElementById(idDiv).style.background = 'orange';
            } else {
               document.getElementById(idDiv).style.background = 'white';
            }
}
var listOfDivId = new Array();
function inseretToArray(idDiv) {
            if ($.inArray(idDiv, listOfDivId) == -1) {
                listOfDivId.unshift(idDiv);
            } else {
                listOfDivId.remove(($.inArray(idDiv, listOfDivId)));
            }
}
</script>

אין תגובות:

הוסף רשומת תגובה