Javascript 如何在没有事件的情况下调用js函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7168323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:08:11  来源:igfitidea点击:

how to call js function without event

javascriptjqueryhtmleventsfunction

提问by Colleen

How can I just call a js function from within an html file, with no event trigger? I want to have code like:

如何在没有事件触发器的情况下从 html 文件中调用 js 函数?我想要这样的代码:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="jquery.flot.js"></script>
<script src="chart.js"></script>

<title>
</title>
</head>

<body>

<div id="chart1" style="width:600px;height:300px"></div>

show_graph({{ chart_type }}, {{ data }}, {{ options }});

</body>

</html>

but this just results in the function call being printed to the screen, instead of the function actually being executed.

但这只会导致函数调用被打印到屏幕上,而不是实际执行的函数。

e.g. I'm getting show_graph(bar, [[1, 2000], [2, 50], [3, 400], [4, 200], [5, 5000]], ['Foo']);

例如我得到 show_graph(bar, [[1, 2000], [2, 50], [3, 400], [4, 200], [5, 5000]], ['Foo']);

What do I do?

我该怎么办?

EDIT:

编辑:

I appreciate the feedback, but I tried wrapping it in a script tag and got an "invalid number of parameters" error.

我感谢反馈,但我尝试将其包装在脚本标签中,但出现“参数数量无效”错误。

the javascript is:

javascript是:

function show_graph(charttype, data, options){

    var chart_options = {
        series: {
            charttype: {
                show: true
            }
        }
    }

    var plot = $.plot($("#chart1"), [data], [chart_options]);
}

so I suppose the real question is "why am I getting an "invalid number of parameters" error when I'm passing 3 parameters and accepting 3 parameters?"

所以我想真正的问题是“当我传递 3 个参数并接受 3 个参数时,为什么我会收到“无效的参数数量”错误?”

回答by user113716

Wrap it in <script>tags:

将其包裹在<script>标签中:

<body>

<div id="chart1" style="width:600px;height:300px"></div>

<script type="text/javascript">
    show_graph({{ chart_type }}, {{ data }}, {{ options }});
</script>

</body>

...though I don't know how the template factors in. I imagine it will render the same.

...虽然我不知道模板是如何影响的。我想它会呈现相同的效果。

回答by Dementic

Yet another answer:

还有一个答案:

<script type="text/javascript">
    (function() {
        // The following code will be enclosed within an anonymous function
        var foo = "Goodbye World!";
        document.write("<p>Inside our anonymous function foo means '" + foo + '".</p>');
    })(); // We call our anonymous function immediately
</script>

回答by Joe Landsman

You need to wrap the function call in script tags like this:

您需要将函数调用包装在脚本标签中,如下所示:

<script type="text/javascript">
show_graph({{ chart_type }}, {{ data }}, {{ options }});
</script>

If you need to validate as XHTML you should also wrap with CDATA. See When is a CDATA section necessary within a script tag?for a reference to that.

如果您需要验证为 XHTML,您还应该使用 CDATA 进行包装。请参阅脚本标记中何时需要 CDATA 部分?供参考。

回答by Tim Withers

Put it in a <script>tag.

把它放在一个<script>标签里。

<script type='text/javascript'>show_graph({{ chart_type }}, {{ data }}, {{ options }});</script>

<script type='text/javascript'>show_graph({{ chart_type }}, {{ data }}, {{ options }});</script>

回答by j3ffz

Add script tags.

添加脚本标签。

<script>
show_graph({{ chart_type }}, {{ data }}, {{ options }});
</script>

回答by Dan Mazzini

You should call the function when the page is loaded. To do so with jQuery use this code:

您应该在加载页面时调用该函数。要使用 jQuery 执行此操作,请使用以下代码:

<script type="text/javascript">
  $(document).ready(function() {
    show_graph({{ chart_type }}, {{ data }}, {{ options }});
  });
</script>