javascript - 如何实现 IE8<= 的 node.localName 等效项?

标签 javascript jquery internet-explorer local-storage

我想向我的库添加一些旧浏览器支持 garlicjs.org .

我发现我的主要函数之一 getPath 依赖于 IE8 不支持的 localName<=

在该函数中,我需要为我的 lib 绑定(bind)​​的每个 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;
}

最佳答案

好的,我已经使用了 nodeName 属性,并且它正在工作。

关于javascript - 如何实现 IE8<= 的 node.localName 等效项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13489224/

相关文章:

jquery - 绑定(bind) native html5 dragstart 事件处理程序并使用 jQuery 访问 dataTransfer

javascript - 需要串起来吗?将对象数组从 javascript 发送到 PHP 的数据格式困惑

javascript - Internet Explorer EDGE 兼容性问题?

javascript - 如何将 jquery.validate 与 requirejs 一起使用?

javascript - 固定标题和第一列的水平和垂直滚动

javascript - AngularJS:指令将对象添加到 $scope 但在 Controller 中未定义

jquery - 在append()之前获取css()属性?

php - IE 主页上的生长字母

javascript - jQuery keypress() 事件没有触发?

javascript - 我怎样才能按原样打印页面