簡體   English   中英

在jquery中處理動態生成的html元素

[英]Manipulate a dynamically generated html element in jquery

我這里有點問題。 ¿我如何在Jquery中操縱動態生成的html?

我有一個像這樣的功能:

generatesomething : function(DestinationID,data){
    result = $.DoSomething(data)
    $('#'+Destinationid).html(data);
}

另一方面,腳本通過ajax接收數組。 自然,我將像這樣迭代數組:

$.each(response, function(key, value){
    ThisHtml = '<div id="div'+key'"></div>';
    $('#MyPlaceHolderDiv').html(ThisHTML)
    //In this point, i really need to call my first function
    $.generatesomething('div'+key',data)
    //But not works!!!!
}

我如何使用函數操縱生成的div?

提前致謝!

編輯:在嘗試澄清我的問題,我將粘貼確切的功能。

我做了這個功能。 請不要嘲笑我的代碼,我是jquery的新手。

jQuery.fn.extend({/ funciones Generales /

piegraph : function(GraficoDestino,tipo,arrayDatos,dato1,dato2,tooltiptemplate,labeltemplate){
    var dataPoints = [];
    $.each(arrayDatos, function(key, value){
        var temporal = {};
        temporal.label = value[dato1];
        temporal.y = parseInt(value[dato2]);
        dataPoints.push(temporal);
    });
    var opciones = {

        animationEnabled : true,
        data : [{
            type : tipo,
            startAngle : 0,
            toolTipContent : tooltiptemplate,
            indexLabel : labeltemplate,
            dataPoints : dataPoints,
        }]

    };
    $('#' + GraficoDestino).CanvasJSChart(opciones);
}

此功能效果很好...如果我可以給它目的地div。

在腳本的其他部分,我有一個ajax調用:

Generadisco: function(){
    var datos = {
        "accion":"generadisco"
    };
    $.ajax({   
        type: "POST",
        url: "blahblah.php",
        data: datos,
        dataType: "json",
        success:function(response){

            $.each(response, function(key, value){
                esteHTML = '<div id="divdisco'+key+'"></div>                    
                $('#discosplace').append(estehtml);
                //the div is generated... but when i do...:
                $(this).piegraph('divdisco'+key,'pie', response[3],0,1, "{label} #percent%","{label} ");
                //nothing happens
            });
        }
    });
}

我在您的代碼中發現了一些錯誤:

$('#MyPlaceHolderDiv').html(ThisHTML)
$.generatesomething('div'+ key ,data)

必須是:

$('#MyPlaceHolderDiv').html(ThisHTML);
$.generatesomething('div'+key',data);

另外,請嘗試在函數的第一行中添加console.log(DestinationID)以查看傳遞的參數(DestinationID)

如果要在ajax調用之后生成動態元素,請嘗試使用async:false。

$ .ajax({url:“ abcd.html”,async:false})。then(function())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM