将onclick值返回给JavaScript函数 [英] return onclick value to a JavaScript function
问题描述
我已经创建了一个带有onclick事件的锚,该事件调用了JavaScript函数. JavaScript函数返回一些值.我想在另一个JS函数中使用该值.
I have created an anchor with an onclick event which calls a JavaScript function. The JavaScript function returns some value. I want to use that value in another JS function.
例如
loading()
将返回一些值,该值将传递给另一个js函数.如何捕获和存储返回值,然后将此值传递给该函数?
e.g
loading()
will return some value which will get passed to another js function. How do I capture and store the return value, and then pass this value to that function?
推荐答案
您可以简单地用内部函数调用外部函数吗?
Can you simply call the outer function with the inner function?
function outerFunc(a)
{
alert(a);
}
function innerFunc()
{
return 'test';
}
onclick="outerFunc(innerFunc());"
或者,如果您需要在另一个事件中使用返回值,请设置一个变量.
Or, if you need to use the return value in another event, set a variable.
var retval;
function outerFunc()
{
if(retval) alert(retval);
}
function innerFunc()
{
retval = 'test';
return retval;
}
onclick="return innerFunc();"
在其他onclick事件中
In someother onclick event
onclick="return outerFunc();"
这篇关于将onclick值返回给JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!