.html() 帮助不知道如何处理它

.html() help dont know how to handle it

本文关键字:处理 何处理 不知道 帮助 html      更新时间:2023-09-26
...
success: function (reqCode) {
            if (reqCode['error_code'] == 1) {
                //Generiere Tabelle     
                $(".done").html( 
                    '<p class="bold center"><?php echo "Besucher ".$month_name[' + reqCode['month'] + ']." ".' + reqCode['year'] + '; ?></p>'
                    '<canvas id="cvs" width="680" height="250">[No canvas support]</canvas>'
                    '<script>'
                        'chart = new RGraph.Line("cvs", ' + reqCode['data_string'] + ');'
                        'chart.Set("chart.tooltips", ' + reqCode['labels_string'] + ');'
                        'chart.Set("chart.tooltips.effect", "expand");'
                        'chart.Set("chart.background.grid.autofit", true);'
                        'chart.Set("chart.gutter.left", 35);'
                        'chart.Set("chart.gutter.right", 5);' 
                        'chart.Set("chart.hmargin", 10);' +
                        'chart.Set("chart.tickmarks", "circle");'
                        'chart.Set("chart.labels", ' + $reqCode['labels_tooltip'] + ');'
                        'chart.Draw();'
                    '</script>'
                );      
                $('.done').fadeOut('slow'); 
                $('.done').fadeIn('slow');
            }   
}   

我不知道为什么每条新行都需要自己的".."。无论如何它不起作用。查看了 API 参考,但没有发现任何有用的:(

编辑:对于我的第二个问题:

这是 JSON 响应:

$response['error_code'] = '1'; 
    $response['data_string'] = "[" . join(", ", $data) . "]";
    $response['labels_string'] = "['" . join("', '", $labels) . "']";
    $response['labels_tooltip'] = "['" . join("', '", $data) . "']";
    $response['month'] = $month_name[$month];
    $response['year'] = $year;
    echo json_encode($response);

那个<script>标签似乎确实有问题,但真的吗? 您无需插入该<script>标记。 你已经在运行JavaScript了;只需这样做:

success: function (reqCode) {
    if (reqCode['error_code'] == 1) {
        var month_name = <?php echo json_encode($month_name); ?>;
        //Generiere Tabelle     
        $(".done").html( 
            '<p class="bold center">Besucher ' + month_name[reqCode['month']] + ' ' + reqCode['year'] + '</p>'+
            '<canvas id="cvs" width="680" height="250">[No canvas support]</canvas>'
        );
        var chart = new RGraph.Line("cvs", reqCode['data_string']);
        chart.Set("chart.tooltips", reqCode['labels_string']);
        chart.Set("chart.tooltips.effect", "expand");
        chart.Set("chart.background.grid.autofit", true);
        chart.Set("chart.gutter.left", 35);
        chart.Set("chart.gutter.right", 5); 
        chart.Set("chart.hmargin", 10);
        chart.Set("chart.tickmarks", "circle");
        chart.Set("chart.labels", reqCode['labels_tooltip']);
        chart.Draw();
        $('.done').fadeOut('slow'); 
        $('.done').fadeIn('slow');
    }   
}   

我已经修复了一些语法错误,尽管我不能保证没有剩下的语法错误。 只需观察 JavaScript 控制台中的错误。

你应该有一个容器元素,上面有类,比如:

<div class="done" />

此外,您可以缩短:

$('.done').html('..all the html..').fadeOut('slow').fadeIn('slow');

此外,正如 Vivin Paliath 所说,您应该使用 + 连接 html 中的所有字符串

'<a>'+
'asdsad'+
'</a>'

祝你好运!

你需要+符号


success: function (reqCode) {
            if (reqCode['error_code'] == 1) {
                //Generiere Tabelle     
                $(".done").html('<p class="bold center"></p>'+
                    '<canvas id="cvs" width="680" height="250">[No canvas support]'+
                    '<script>'+
                        'chart = new RGraph.Line("cvs", ' + reqCode['data_string'] + ');'+
                        'chart.Set("chart.tooltips", ' + reqCode['labels_string'] + ');'+
                        'chart.Set("chart.tooltips.effect", "expand");'+
                        'chart.Set("chart.background.grid.autofit", true);'+
                        'chart.Set("chart.gutter.left", 35);'+
                        'chart.Set("chart.gutter.right", 5);' +
                        'chart.Set("chart.hmargin", 10);' +
                        'chart.Set("chart.tickmarks", "circle");'+
                        'chart.Set("chart.labels", ' + $reqCode['labels_tooltip'] + ');'+
                        'chart.Draw();'+
                    ''
                );      
                $('.done').fadeOut('slow'); 
                $('.done').fadeIn('slow');
            }   
}  

您需要将单个字符串传递给 html() 函数。像这样:

$(".done").html("<p>hello</p><p>goodbye</p>") .

如果需要组合多个字符串,则需要使用字符串连接,例如:

var combinedString = '<p>hello</p>' + '<p>goodbye</p>'