Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从html代码中调用包含回调函数的函数?_Javascript_Html - Fatal编程技术网

Javascript 如何从html代码中调用包含回调函数的函数?

Javascript 如何从html代码中调用包含回调函数的函数?,javascript,html,Javascript,Html,我有一个html文件,其中包含一个“onclick”事件,该事件调用一个javascript函数,该函数还包含一个回调函数。我认为语法有问题,因为我无法执行回调函数。有人能给我建议吗? 我在“开放(x)”处收到“非法调用” html: .js 函数friendReq(x,回调){ var section_p=document.getElementById(x); var option_p=document.getElementById('options_div'); var maxH='30

我有一个html文件,其中包含一个“onclick”事件,该事件调用一个javascript函数,该函数还包含一个回调函数。我认为语法有问题,因为我无法执行回调函数。有人能给我建议吗? 我在“开放(x)”处收到“非法调用”

html:


.js

函数friendReq(x,回调){
var section_p=document.getElementById(x);
var option_p=document.getElementById('options_div');
var maxH='300px';
var max_op='200px';
//如果“选项”处于打开状态,请先将其关闭。
如果(选项p.style.height==最大值){
选项p.style.height='0px';

option_p.style.transition='height 0.8s';首先,您有一个输入错误:

 <div id="div_friend"><img id="friend" class= "image_icon" src="images/friends_icon.png"  onclick="friendReq('sections_panel', 'open'"></div>
在此之后,我相信您的代码应该正确地调用
open
函数

编辑以使思考更清晰一点。 因此,您可以从html调用
friendReq('sections\u panel','open');
,其中“open”实际上不是一个函数,也不是对open函数的引用,而是一个简单的字符串。现在,看看您的代码设计,您不需要将open函数作为回调参数传递,因为它们都在同一个作用域中,并且彼此可见。因此,您可以简单地执行以下操作:

function friendReq(x){
var section_p = document.getElementById(x);
var option_p = document.getElementById('options_div');
var maxH = '300px';
var max_op = '200px';


//IF 'options_div' IS OPEN, CLOSE IT FIRST.
if(option_p.style.height == max_op){
    option_p.style.height = '0px';
    option_p.style.transition = 'height 0.8s';  <---code works up to here.

    open(x); // callback function to execute when above code has finnished.DOES NOT EXECUTE.
}else{ // This section works fine.
 //rest of your code  
}

}

function open(id){
var section_p = document.getElementById(id);
var maxH = '300px';

section_p.style.height=maxH;
section_p.style.transition = 'height 0.7s';

}
函数friendReq(x){
var section_p=document.getElementById(x);
var option_p=document.getElementById('options_div');
var maxH='300px';
var max_op='200px';
//如果“选项”处于打开状态,请先将其关闭。
如果(选项p.style.height==最大值){
选项p.style.height='0px';

option_p.style.transition='height 0.8s';我已经更正了我的输入错误,但是现在我得到了'open is not a function'错误。检查我编辑的其余部分,只是评论了为什么你不能调用open函数。很抱歉,我正在努力理解你写的关于不添加参数的内容。我不理解函数fri是如何实现的endReq(x){…}可以调用回调函数,而不需要将回调函数作为参数添加。只要调用的函数具有“回调函数”的可见性,就不需要将函数作为回调传递,我看到您在friendReq函数下面定义了open函数,这两个函数都在同一个作用域中,因此它们可以相互看到。通过在函数friendReq中定义一个名为“open”的参数,在friendReq的作用域内,open将是该参数。从html中,您将传递一个字符串作为该参数,因此,您将变量open设置为friendReq函数作用域内的字符串。因此html代码保持不变:onclick=“friendReq('sections_panel','open');”?
 <div id="div_friend"><img id="friend" class= "image_icon" src="images/friends_icon.png"  onclick="friendReq('sections_panel', 'open'"></div>
 <div id="div_friend"><img id="friend" class= "image_icon" src="images/friends_icon.png"  onclick="friendReq('sections_panel', 'open');"></div>
function friendReq(x){
function friendReq(x){
var section_p = document.getElementById(x);
var option_p = document.getElementById('options_div');
var maxH = '300px';
var max_op = '200px';


//IF 'options_div' IS OPEN, CLOSE IT FIRST.
if(option_p.style.height == max_op){
    option_p.style.height = '0px';
    option_p.style.transition = 'height 0.8s';  <---code works up to here.

    open(x); // callback function to execute when above code has finnished.DOES NOT EXECUTE.
}else{ // This section works fine.
 //rest of your code  
}

}

function open(id){
var section_p = document.getElementById(id);
var maxH = '300px';

section_p.style.height=maxH;
section_p.style.transition = 'height 0.7s';

}