div中循環生成button並添加點擊事件

回看剛剛學習js的博文,頁面循環生成節點是頁面最基礎的操作之一。
要讓頁面顯示效果,必然在頁面添加dom元素!如果想要多個button,那麼循環添加必不可少。

首先定義div:

<div id="people" style="border: groove; padding-top:1px;width: 58%;height: 253px;overflow-y:auto;"></div>  

over-flow-y可以開啓y軸滾動條
循環添加button的 js代碼:

//循環在div中添加button
function addButton(){
    var people = $('#subContent').data('sendPeople'),str="";
    for(var i=0; i<people.length; i++){
        var name = people[i].name+" "+"x";
        str+='<input type="button" style="margin:8px;width:84px" class="btn btn-default" name="'+"sendPeople"+'" id="'+i+'"  value="'+name+'" />';
        }
    document.getElementById("people").innerHTML=str;
    //$('#people').html(str)
}

1.測試用的數據,採用JQuery的存儲方式。
2.如果用js原生語言可以將方法改爲傳遞參數類型:addButton(people)
3.document.getElementById(“people”).innerHTML=str;用於生成str語句中的內容,innerHTML爲覆寫操作,如果想在頁面添加而不影響原本的元素,可以用append操作。
4.document.getElementById("people").innerHTML=str;是js原生寫法,等同於$("#people").html(str); 這是JQuery寫法。同樣可以實現。
但是有趣的是 $("#people").innerHTML=str並不能運行,看來
$(“#people”)並不完全等同document.getElementById(“people”)。
這是因爲JQuery是DOM的封裝和拓展,並不完全等於dom元素。

var testPeople = [
{"id":"1","name":"李1"},{"id":"2","name":"李大龍2"},{"id":"3","name":"李麗麗3"},{"id":"4","name":"李4"},{"id":"5","name":"李5"},{"id":"6","name":"王6"},{"id":"7","name":"王薩德7"},{"id":"8","name":"王三單8"},{"id":"9","name":"王9"},{"id":"10","name":"王10"}];

效果圖:
這裏寫圖片描述
拓展使用:添加點擊事件/添加自定義屬性

//循環在div中添加button
function addButton(){
    var people = $('#subContent').data('sendPeople'),
        str="";
    for(var i=0,len=people.length; i<len; i++){
        var name = people[i].name+" "+"x";
        str+='<input type="button" style="margin:8px;width:84px" class="btn btn-default" name="'+"sendPeople"+'" id="'+i+'"  value="'+name+'" />';
    }
    document.getElementById("people").innerHTML=str;
    //自定義屬性存儲people
    for(var j=0; j<people.length; j++){
        var peopleId = people[j].id;
        $('#'+j).attr('peopleId',peopleId);
    }
    btnClick();
}

點擊事件:

function btnClick(){
    var people = $('#subContent').data('sendPeople');
    $("input[name='sendPeople']").off('click').on('click',function(){
        var indexId = $(this).attr("id"),
            name = people[indexId].name,
            peopleId = $('#'+indexId).attr('peopleId');
        $.confirm({
            title: '警告: ',
            content: '確認刪除"'+name+'"該收件人',
            buttons: {
                ok: {
                    text: "確定",
                    btnClass: 'btn-sm',
                    keys: ['enter'],
                    action: function(){
                    people.splice(indexId, 1);                      $('#subContent').data('sendPeople',people);
                        addButton();
                    }
                },
                cancel: { 
                    text: "取消",
                    btnClass: 'btn-sm',
                    action: function(){                         
                    }
                }
            }
        });
    });
}

1.利用定義時,html中input name標籤可以同名。JQuery可以根據name選取所有的name=“sendPeople”的標籤並定義點擊事件。注意要先off再on避免重複定義方法。
2.利用splice(indexId, num),indexId=刪除的位置,num=刪除個數。
3.利用html5自定義屬性,$('').attr("peopleId",people)存儲數據,同樣可以存儲對象。
4.在點擊事件觸發時,利用$(this).attr("id")取出觸發點擊事件的id。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章