1
我想添加一些舊的瀏覽器支持我的庫文件garlicjs.org。如何實現IE8等效的node.localName <=?
我發現我的主要功能getPath
一個依賴於localName
即上IE8 <不受支持=
在這種功能,我需要獲取唯一標識符字符串用於通過我的LIB綁定每個DOM元素,更重要的是,不依賴於ID,類別或名稱。
我可以管理:爲舊的IE瀏覽器支持這個屬性,或者使用所有瀏覽器支持的另一個屬性,可以管理這個屬性,它具有與此功能相同的效果。
謝謝!
下面是代碼:
/* retuns an unique identifier for form elements, depending on their behaviors:
* radio buttons: domain > pathname > form.<attr.name>[:eq(x)] > input.<attr.name>
no eq(); must be all stored under the same field name inside the same form
* checkbokes: domain > pathname > form.<attr.name>[:eq(x)] > [fieldset, div, span..] > input.<attr.name>[:eq(y)]
cuz' they have the same name, must detect their exact position in the form. detect the exact hierarchy in DOM elements
* other inputs: domain > pathname > form.<attr.name>[:eq(x)] > input.<attr.name>[:eq(y)]
we just need the element name/eq() inside a given form
*/
, getPath: function() {
// Requires one element.
if (this.$element.length != 1) {
return false;
}
var path = ''
, fullPath = this.$element.is('input[type=checkbox]')
, node = this.$element;
while (node.length) {
var realNode = node[0]
, name = realNode.localName;
if (!name) {
break;
}
name = name.toLowerCase();
var parent = node.parent()
, siblings = parent.children(name);
// don't need to pollute path with select, fieldsets, divs and other noisy elements,
// exept for checkboxes that need exact path, cuz have same name and sometimes same eq()!
if (!$(realNode).is('form, input, select, textarea') && !fullPath) {
node = parent;
continue;
}
// set input type as name + name attr if exists
name += $(realNode).attr('name') ? '.' + $(realNode).attr('name') : '';
// if has sibilings, get eq(), exept for radio buttons
if (siblings.length > 1 && !$(realNode).is('input[type=radio]')) {
name += ':eq(' + siblings.index(realNode) + ')';
}
path = name + (path ? '>' + path : '');
// break once we came up to form:eq(x), no need to go further
if ('form' == realNode.localName) {
break;
}
node = parent;
}
return 'garlic:' + document.domain + (this.options.domain ? '*' : window.location.pathname) + '>' + path;
}