0

我有一个元素"<a id="sample" jquery1234567="0">Testing</a>"

我想从上面的标签中删除最后一个属性(jquery1234567)。但是属性“jquery1234567”将动态生成。

4

3 回答 3

2

此生成属性的值为$.expando。因此,如果要删除它,只需使用:

$('#sample').removeAttr($.expando);

虽然我不建议删除此属性,因为 jQuery 使用它来跟踪绑定到 DOM 元素的事件处理程序。影响此行为的正确方法是使用 jQuery 解除与该元素的事件绑定。

于 2013-11-05T06:51:31.727 回答
0

试试这个

    $.fn.getRegexAttr = function(regex, action) {

    if( !this.length ) return false;

    if( 'remove' == action ){

        var that = this;
        $(this[0].attributes)
            .filter(function(){ return regex.test(this.name); })
            .each(function(){ $(that).removeAttr(this.name); });


    }else if( 'fetch' == action ){

    return $(this[0].attributes)
            .filter(function(){ return regex.test(this.name); })
            .map(function(){ return $.trim(this.name); })
            .get();
    }
}

if( $('#sample').getRegexAttr(/jquery[0-9]/, 'fetch') ) alert( $('#sample').getRegexAttr(/jquery[0-9]/, 'fetch').join(',') );// To get the matched attributes

$('#sample').getRegexAttr(/jquery[0-9]/, 'remove');// To remove the matched attributes

你也可以验证:http: //jsfiddle.net/Nng8n/3/

于 2012-02-25T03:04:43.517 回答
-1

试试这个

  $("#sample").removeAttr('jquery1234567');
                (or)
  var id=1234567;
  $("#sample").removeAttr('jquery'+id);

这对你没有帮助..请用代码添加一些额外的描述......

嗨朋友这会帮助你

      lastAttrIndex=($("#sample")[0].attributes.length)-1;
      lastAttr=$("#sample")[0].attributes[lastAttrIndex].name;
      $("#sample").removeAttr(lastAttr);
于 2012-02-23T12:12:51.903 回答