JS---DOM --- common.js involved in the course

Posted by zTagged on Wed, 11 Dec 2019 15:18:25 +0100


//Code to format the date //Get the specified label object //Get the text content of the element //Get the text content of the element //Get the first child element in the parent element //Get the last child element in the parent element //Get the previous sibling of an element //Get the next sibling of an element //Get all siblings of an element //Returns the type of browser the current browser is //Binding event for any element: element, event type, event handler //Unbind an event for any element: element, event type, event handler

/**
 * format date
 * @param dt Date object
 * @returns {string} The return value is the formatted string date
 */
function getDates(dt) {
    var str = "";//String to store time
    //Year of acquisition
    var year = dt.getFullYear();
    //Acquisition month
    var month = dt.getMonth() + 1;
    //Acquisition date
    var day = dt.getDate();
    //Acquisition hours
    var hour = dt.getHours();
    //Get minutes
    var min = dt.getMinutes();
    //Get seconds
    var sec = dt.getSeconds();
    month = month < 10 ? "0" + month : month;
    day = day < 10 ? "0" + day : day;
    hour = hour < 10 ? "0" + hour : hour;
    min = min < 10 ? "0" + min : min;
    sec = sec < 10 ? "0" + sec : sec;
    str = year + "year" + month + "month" + day + "day " + hour + ":" + min + ":" + sec;
    return str;
}
/**
 * Get the specified label object
 * @param id id attribute value of label
 * @returns {Element}Returns the specified label object according to the id attribute value
 */
function my$(id) {
    return document.getElementById(id);
}

function setInnerText(element, text) {
    if (typeof element.textContent == "undefined") {
        element.innerText = text;
    } else {
        element.textContent = text;
    }
}
/**
 * Get the text content of the element
 * @param element Arbitrary elements
 * @returns {*} Text content in any element
 */
function getInnerText(element) {
    if (typeof (element.textContent) == "undefined") {
        return element.innerText;
    } else {
        return element.textContent;
    }
}
/**
 * Get the first child element in the parent element
 * @param element Parent element
 * @returns {*} Child element in parent element
 */
function getFirstElement(element) {
    if (element.firstElementChild) {
        return element.firstElementChild;
    } else {
        var node = element.firstChild;
        while (node && node.nodeType != 1) {
            node = node.nextSibling;
        }
        return node;
    }
}
/**
 * Get the last child element in the parent element
 * @param element Parent element
 * @returns {*} Last child element
 */
function getLastElement(element) {
    if (element.lastElementChild) {
        return element.lastElementChild;
    } else {
        var node = element.lastChild;
        while (node && node.nodeType != 1) {
            node = node.previousSibling;
        }
        return node;
    }
}
/**
 * Get the previous sibling of an element
 * @param element Some element
 * @returns {*} Previous sibling element
 */
function getPreviousElement(element) {
    if (element.previousElementSibling) {
        return element.previousElementSibling
    } else {
        var node = element.previousSibling;
        while (node && node.nodeType != 1) {
            node = node.previousSibling;
        }
        return node;
    }
}
/**
 * Get the next sibling of an element
 * @param element Some element
 * @returns {*} The next sibling element
 */
function getNextElement(element) {
    if (element.nextElementSibling) {
        return element.nextElementSibling
    } else {
        var node = element.nextSibling;
        while (node && node.nodeType != 1) {
            node = node.nextSibling;
        }
        return node;
    }
}
/**
 * Get all siblings of an element
 * @param element Some element
 * @returns {Array} Brotherhood
 */
function getSiblings(element) {
    if (!element) return;
    var elements = [];
    var ele = element.previousSibling;
    while (ele) {
        if (ele.nodeType === 1) {
            elements.push(ele);
        }
        ele = ele.previousSibling;
    }
    ele = element.nextSibling;
    while (ele) {
        if (ele.nodeType === 1) {
            elements.push(ele);

        }
        ele = ele.nextSibling;
    }
    return elements;
}
/**
 * Returns the type of browser the current browser is
 */
function userBrowser() {
    var browserName = navigator.userAgent.toLowerCase();
    if (/msie/i.test(browserName) && !/opera/.test(browserName)) {
        console.log("IE");
    } else if (/firefox/i.test(browserName)) {
        console.log("Firefox");
    } else if (/chrome/i.test(browserName) && /webkit/i.test(browserName) && /mozilla/i.test(browserName)) {
        console.log("Chrome");
    } else if (/opera/i.test(browserName)) {
        console.log("Opera");
    } else if (/webkit/i.test(browserName) && !(/chrome/i.test(browserName) && /webkit/i.test(browserName) && /mozilla/i.test(browserName))) {
        console.log("Safari");
    } else {
        console.log("I don't know what the hell!");
    }
}



//Binding events for any element:element,Event type,Event handler
function addEventListener(element, type, fn) {
    if (element.addEventListener) {
        //Support
        element.addEventListener(type, fn, false);
    } else if (element.attachEvent) {
        element.attachEvent("on" + type, fn);
    } else {
        element["on" + type] = fn;
    }
}
//Unbind an event for any element:element,Event type,Event handler
function removeEventListener(element, type, fn) {
    if (element.removeEventListener) {
        element.removeEventListener(type, fn, false);
    } else if (element.detachEvent) {
        element.detachEvent("on" + type, fn);
    } else {
        element["on" + type] = null;
    }
}

Topics: Javascript Attribute Firefox IE