【问题标题】:Get all attribute properties [duplicate]获取所有属性属性[重复]
【发布时间】:2013-04-05 21:33:28
【问题描述】:

我想通过 Jquery 事件获取所有属性属性。我知道我可以通过执行以下操作来检索元素的信息:

$(this).attr('id');

因此,如果我想包含该课程,我会这样做:

$(this).attr('id');
$(this).attr('class');

但是我想尽可能地缩短/简化代码,所以我想知道是否可以在不先指定它们的情况下一次获取所有属性属性。有没有办法做到这一点?也许是插件?

【问题讨论】:

  • 我查看了副本中提供的答案,它以@​​987654325@ 开头,这意味着一些信息已经知道以开头。在我的代码中不是这种情况,我绝对没有任何信息可以开始,但想在点击时获得它
  • @Youss,在 click 处理程序中,this 将绑定到被点击的元素,因此您根本不需要 getElementById()
  • @Frédéric Hamidi 对我不起作用(链接)JsFiddle
  • @Youss,你小提琴中的代码没有定义任何click处理程序。
  • @Frédéric Hamidi 这有多重要? jsfiddle.net/yeZrC/2

标签: javascript jquery properties attributes


【解决方案1】:

这是一个例子:

<div id="test" class="test" type="test"/>

// select the test element and bind the handler
$('#test').click(function(){
    // loop over it's attributes on click
    for(var i = 0;i < this.attributes.length;i++){
        console.log(this.attributes[i]);
    }
});

演示:http://jsfiddle.net/louisbros/75w7y/

【讨论】:

    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 2021-01-09
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 2012-11-11
    相关资源
    最近更新 更多