jQuery 获取/设置/删除DOM元素的属性以a元素为例

  jQuery的属性操作非常简单,下面以一个a元素来说明属性的获取/设置/删除操作

  

复制代码 代码如下:

  <body>

  <a>jquery.com</a>

  </body>

  添加属性

  $('a').attr('href', 'http://www.jquery.com')

  添加多个属性

  $('a').attr({'href':'http://www.jquery.com', 'title':'jquery.com'})

  获取属性

  $('a').attr('href')

  class属性

  addClass()

  添加一个class

  hasClass()

  判断是否有参数中指定的class

  removeClass()

  移除一个class

  toggleClass()

  如果参数中指定的class已经存在,则删除,反之,则添加。