Javascript 在多个函数中访问函数的值

Javascript 在多个函数中访问函数的值,javascript,jquery,Javascript,Jquery,tl;dr:如何在其他函数中重用第一个函数?如果在其他函数中调用,它将继续返回未定义 我创建在线帮助(我不是程序员),不幸的是,它是由Adobe RoboHelp以框架集输出的。我想使用下面的第一个函数(getURL)动态构建一个可以在其他函数中重用的URL。例如,在一个函数中将“a”参数作为图形传递,或者在另一个函数中将其作为mailto:链接发送框架集中的页面 我遇到的问题是从其他函数中调用getURL函数;正在返回未定义的fullURL值 function getURL(a) {

tl;dr:如何在其他函数中重用第一个函数?如果在其他函数中调用,它将继续返回未定义

我创建在线帮助(我不是程序员),不幸的是,它是由Adobe RoboHelp以框架集输出的。我想使用下面的第一个函数(getURL)动态构建一个可以在其他函数中重用的URL。例如,在一个函数中将“a”参数作为图形传递,或者在另一个函数中将其作为mailto:链接发送框架集中的页面

我遇到的问题是从其他函数中调用getURL函数;正在返回未定义的fullURL值

function getURL(a) {
    var frameURL = window.frames[1].frames[1].document.location, 
    frameareaname = frameURL.pathname.split('/').slice(4, 5), 
    frameprojname = frameURL.pathname.split('/').slice(6, 7),
    protocol_name = window.location.protocol,
    server_name = window.location.host,
fullURL = protocol_name + '//' + server_name + '/robohelp/robo/server/' + frameareaname + '/projects/' + frameprojname + '/' + a;
return fullURL;
}
如果我像这样调用函数,它可以正常工作,但如果我将其放在函数中,则不会:

 getURL('light_bulb.png');
 console.log(fullURL);
如何在其他函数中重用此函数?例如,fullURL应为背景图像:

  $('.Graphic, .GraphicIndent, .Graphic3rd, .Graphic4th').not('.Graphic-norollover').mouseover(function()
  {
    var imgWidth = $(this).children('img').width();
    $(this).css('background', 'url(' + fullURL + ') 50% 50% no-repeat #000');
    $(this).css('width', imgWidth);
    $(this).children('img').fadeTo(750, '.4');
    $(this).children('img').attr('alt', 'Click to view full-size graphic');
    $(this).children('img').attr('title', 'Click to view full-size graphic');
  });

谢谢

您必须调用该函数才能使用它:

  $('.Graphic, .GraphicIndent, .Graphic3rd, .Graphic4th').not('.Graphic-norollover').mouseover(function()
  {
    var imgWidth = $(this).children('img').width();
    $(this).css('background', 'url(' + getURL('light_bulb.png') + ') 50% 50% no-repeat #000');
    $(this).css('width', imgWidth);
    $(this).children('img').fadeTo(750, '.4');
    $(this).children('img').attr('alt', 'Click to view full-size graphic');
    $(this).children('img').attr('title', 'Click to view full-size graphic');
  });

fullURL
仅限于
getURL
函数的范围。其他地方看不见。您必须调用
getURL
才能获得函数的结果。

您必须调用函数才能使用它:

  $('.Graphic, .GraphicIndent, .Graphic3rd, .Graphic4th').not('.Graphic-norollover').mouseover(function()
  {
    var imgWidth = $(this).children('img').width();
    $(this).css('background', 'url(' + getURL('light_bulb.png') + ') 50% 50% no-repeat #000');
    $(this).css('width', imgWidth);
    $(this).children('img').fadeTo(750, '.4');
    $(this).children('img').attr('alt', 'Click to view full-size graphic');
    $(this).children('img').attr('title', 'Click to view full-size graphic');
  });

fullURL
仅限于
getURL
函数的范围。其他地方看不见。您必须调用
getURL
才能获得函数的结果。

fullURL
是从
getURL
返回的,因此需要值时调用该函数:

var imageURL = getURL('some_image.png');

fullURL
不存在于
getURL
之外
fullURL
是从
getURL
返回的,因此需要值时调用该函数:

var imageURL = getURL('some_image.png');

fullURL
不存在于
getURL

之外,您可能需要
console.log(getURL('light_bulb.png')
您可能需要
console.log(getURL('light_bulb.png')我尝试了这个,但在测试中似乎不起作用。我想我会再试一次;我认为帧使它变得格外困难。我试过了,但在我的测试中似乎不起作用。我想我会再试一次;我认为帧使它变得更加困难。谢谢,我知道我必须在某个地方调用函数,只是不知道在哪里!这似乎有效。谢谢,我知道我必须在某处调用函数,只是不知道在哪里!这似乎奏效了。