在javascript中包装函数

时间:2013-07-10 09:24:51

标签: javascript jquery asp.net

如何将函数包装到另一个函数中,以便我可以在后面的代码中调用它?

所以这是我需要包装并将其调用到函数的代码:

$(function () { 
$('#container').highcharts({
    chart: {
        type: 'bar'
    },
    title: {
        text: 'Fruit Consumption'
    },
    xAxis: {
        categories: ['Apples', 'Bananas', 'Oranges']
    },
    yAxis: {
        title: {
            text: 'Fruit eaten'
        }
    },
    series: [{
        name: 'Jane',
        data: [1, 0, 4]
    }, {
        name: 'John',
        data: [5, 7, 3]
    }]
    });
 });​

<div id="container" style="width:340px; height:270px;"></div>

我被告知我可以将它放入包装然后调用该函数。怎么办呢?

这个数据是假的,我的数据来自服务器。我想要的是重新绘制代码背后的图表,以便使用RegisterClientScriptBlock刷新数据。

3 个答案:

答案 0 :(得分:2)

在aspx页面

<script type="text/javascript">
    function container_highcharts() {
        $('#container').highcharts({
            chart: {
                type: 'bar'
            },
            title: {
                text: 'Fruit Consumption'
            },
            xAxis: {
                categories: ['Apples', 'Bananas', 'Oranges']
            },
            yAxis: {
                title: {
                    text: 'Fruit eaten'
                }
            },
            series: [{
                name: 'Jane',
                data: [1, 0, 4]
            }, {
                name: 'John',
                data: [5, 7, 3]
            }]
        });
    }
</script>

中的代码

protected void Page_Load(object sender, EventArgs e){
     string someScript = "<script language='javascript'>container_highcharts();</script>";
     Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", someScript);
}

答案 1 :(得分:2)

定义以下一般功能:

<script type="text/javascript">
    function container_highcharts(chartTitle, xAxisCategories, yAxisTitle, seriesArray)
    {
        $(document).ready(function()
        {
            $('#container').highcharts({
                chart: {
                    type: 'bar'
                },
                title: {
                    text: chartTitle
                },
                xAxis: {
                    categories: xAxisCategories
                },
                yAxis: {
                    title: {
                        text: yAxisTitle
                    }
                },
                series: seriesArray
            });
        });
    }
</script>

然后在代码隐藏中,使用动态参数注册此函数调用,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
     string chartTitle = GetChartTitle();
     string xAxisCategories = GetXAxisCategories();
     string yAxisTitle = GetYAxisTitle();
     string seriesArray = GetSeriesArrayAsString();

     string someScript = String.Format("<script language='javascript'>container_highcharts({0}, {1}, {2}, {3});</script>", chartTitle, xAxisCategories, yAxisTitle, seriesArray);
     Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", someScript);
}

这将注册您的脚本,该脚本将在页面加载时运行。

答案 2 :(得分:1)

这就是你需要的:

protected void Page_Load(object sender, EventArgs e){
 string someScript = "<script language='javascript'>container_highcharts();</script>";
 Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", someScript);
}