简体   繁体   English

在jquery中处理动态生成的html元素

[英]Manipulate a dynamically generated html element in jquery

I have a little problem here. 我这里有点问题。 ¿How i can manipulate a dynamically generated html, in Jquery? ¿我如何在Jquery中操纵动态生成的html?

I have a function like: 我有一个像这样的功能:

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

The script, in other point, receive through ajax an array. 另一方面,脚本通过ajax接收数组。 Naturally, I will iterate the array like: 自然,我将像这样迭代数组:

$.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!!!!
}

How i can manipulated the generated div using my function? 我如何使用函数操纵生成的div?

Thanks in advance! 提前致谢!

Edit: in a try to clarify my question, i will paste the exact functions. 编辑:在尝试澄清我的问题,我将粘贴确切的功能。

I made this function. 我做了这个功能。 Please do not laugh at my code, I am newbie in jquery. 请不要嘲笑我的代码,我是jquery的新手。

jQuery.fn.extend({ / funciones Generales / 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);
}

This function works pretty well... if i can give it the destination div to it. 此功能效果很好...如果我可以给它目的地div。

In other part of my script, i have a ajax call: 在脚本的其他部分,我有一个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
            });
        }
    });
}

I found some error in your code: 我在您的代码中发现了一些错误:

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

must to be: 必须是:

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

Also try to add console.log(DestinationID) in first line of your function to see passed argument (DestinationID) 另外,请尝试在函数的第一行中添加console.log(DestinationID)以查看传递的参数(DestinationID)

If you are generating the dynamic elements after your ajax call, try using async:false. 如果要在ajax调用之后生成动态元素,请尝试使用async:false。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM