0

我需要为已经有一个类的按钮分配一个 ID。我只能使用该代码分配一个新类:

<script>
(function($) {
    $(document).ready(function() {
        var buttonFancy = $('.header-content .et_pb_more_button.et_pb_button_one');
        buttonFancy.each(function(){
            $(this).addClass('treeee');
        })
    });
})(jQuery)
</script>
4

1 回答 1

0

attr您可以使用(参见https://api.jquery.com/attr/)更改任何属性$(this).attr('id', 'myID' + index);,将 id 更改为带有当前按钮索引的“myID-”。

(function($) {
  $(document).ready(function(index) {
    var buttonFancy = $('.header-content .et_pb_more_button.et_pb_button_one');
    buttonFancy.each(index => {
      $(this).addClass('treeee');
      $(this).attr('id', 'button-' + index);
    })
  });
})(jQuery)
于 2018-09-06T15:58:51.840 回答