使用Jquery动态创建按钮 [英] Dynamically create buttons with Jquery

查看:102
本文介绍了使用Jquery动态创建按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Jquery动态创建10个按钮,并将其上的文本设置为1 -10,并向所有按钮添加相同的click事件。

I need to create 10 buttons dynamically with Jquery and set the text on them to be 1 -10, and add the same click event to all of them.

我应该使用dom create元素还是其他东西?

Should I be using dom create element or something other?

推荐答案



$(document).ready(function() {
  for(i = 1; i <=10; i++) {
     $('<button/>', {
        text: i, //set text 1 to 10
        id: 'btn_'+i,
        click: function () { alert('hi'); }
    });
  }
});

希望有帮助

这篇关于使用Jquery动态创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆