添加具有与按钮对应的链接的属性内容类型

Add attribute content-type with a link that corresponds to the button

本文关键字:链接 属性 类型 按钮 添加      更新时间:2023-09-26

通过编程方式添加具有与按钮对应的链接的属性内容类型的最佳方法是什么?例如,对于第一个按钮添加属性content-type: link,然后对于第二个按钮添加content-type: link2,依此类推

JS-

$('button').attr('content-type', 'link');
$('button').attr('content-type', 'link2');
$('button').attr('content-type', 'link3');

HTML-最终结果应该是什么样子:

<button content-type="link">First Button</button>
<button content-type="link2">Second Button</button>
<button content-type="link3">Third Button</button>

尝试使用.each()之类的有趣工具:

$(document).ready(function(){
   $("button").each(function(i))
   {
      if(i == 0)
      {
         $(this).attr("content-type","link");
      }
      else
      {
         $(this).attr("content-type","link"+(i+1));
      }
   });
});