繁体   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