Dynamically create buttons with Jquery

前端 未结 4 1804
旧时难觅i
旧时难觅i 2020-12-14 03:41

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.

Should I be using dom create e

4条回答
  •  轮回少年
    2020-12-14 03:54

    try this:

    var something = $('').attr({ type: 'button', name:'btn1', value:'a button' });
    

    now append this to your div (in this example, it has the id "item"):

    $("#item").append(something);
    

    of course, for dynamic values, you need to do it inside a loop with iterated values of the name or ID field of the button..

    hope explaining the concept helps :)

提交回复
热议问题