Javascript “如何删除扩展属性”;jquery123456“;来自元素

Javascript “如何删除扩展属性”;jquery123456“;来自元素,javascript,expando,Javascript,Expando,我有一个元素“测试” 我想从上面的标记中删除最后一个属性(jquery1234567)。 但是属性“jquery1234567”将动态生成。试试这个 $("#sample").removeAttr('jquery1234567'); (or) var id=1234567; $("#sample").removeAttr('jquery'+id); $.fn.getRegexAttr = function(regex, action) {

我有一个元素“测试”

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

试试这个

  $("#sample").removeAttr('jquery1234567');
                (or)
  var id=1234567;
  $("#sample").removeAttr('jquery'+id);
    $.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();
    }
}
这对你没有帮助。请用代码添加一些额外的说明

嗨,朋友,这会帮助你的

      lastAttrIndex=($("#sample")[0].attributes.length)-1;
      lastAttr=$("#sample")[0].attributes[lastAttrIndex].name;
      $("#sample").removeAttr(lastAttr);
试试这个

  $("#sample").removeAttr('jquery1234567');
                (or)
  var id=1234567;
  $("#sample").removeAttr('jquery'+id);
    $.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'))
警报($('#sample')。getRegexAttr(/jquery[0-9]/,'fetch')。连接(',');//要获取匹配的属性

$('#sample')。getRegexAttr(/jquery[0-9]/,'remove');//要删除匹配的属性


您还可以在中验证:

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

$('#sample').removeAttr($.expando); $('#sample').removeAttr($.expando);
尽管我不建议删除这个属性,因为jQuery使用它来跟踪绑定到DOM元素的事件处理程序。影响此行为的正确方法是使用jQuery解除此元素的事件绑定。

这是jQuery内部使用的。为什么您认为您需要删除它?为了回答“为什么您认为您需要删除它?”问题的一部分,此动态生成的属性可防止高亮显示/选择IE浏览器中实现它的元素范围内包含的内容。问题应该是,“为什么JQuery需要在IE中实现它?”我们找不到确切的属性。该属性将被动态创建,前缀为jquerywe不能定义varid=1234567;这将是动态内容我真正需要的是删除该特定属性。因为它是在IE中动态生成的,而不是在Firefox中生成的。lastAttrIndex=($(“#示例”)[0]。attributes.length)-1;lastAttr=$(“#样本”)[0]。属性[lastAttrIndex]。名称$(“样本”).removeAttr(lasttr);这是正确的方法,正如罗曼评论的那样,这根本不是一个好的实践。。。