Fungsi jQuery ini mengambil jalan hierarki elemen dalam pokok dom. Ia melintasi unsur -unsur induk sehingga mencapai akar.
, yang dilanjutkan ke prototaip objek jQuery. Fungsi ini secara rekursif memanggil dirinya pada elemen induk sehingga keadaan tertentu dipenuhi. getPath
/* jQuery function to get the hierarchical path of an element */ jQuery.fn.extend({ getPath: function(path) { if (typeof path === 'undefined') path = ''; // Initialize path if undefined let currentElementName = this.get(0).nodeName.toLowerCase(); let id = this.attr('id'); // Add ID to path if available, handling the special case of an element with id 'browser' if (typeof id !== 'undefined') { if (id === 'browser') { return path; // Stop recursion if id is 'browser' } else { path = `${currentElementName}#${id} ` + path; // Add id to path } } else { path = `${currentElementName} ` + path; // Add element name to path } // Recursive call to parent element return this.parent().length ? this.parent().getPath(path) : path.trim(); // Return trimmed path if parent doesn't exist } });
currentElementName
cur
.length
Pembinaan Laluan: .trim()
Dihapus HTML Cek: html.search
Fungsi yang disemak ini menyediakan cara yang lebih dipercayai dan efisien untuk mendapatkan jalan hierarki dari mana -mana elemen dalam konteks jQuery. Atas ialah kandungan terperinci JQuery Dapatkan Jalan Hierarki Elemen. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!