tl; dr:如何在其他功能中重用第一个功能?如果在其他函数中调用,它将保持返回未定义状态。

我创建了在线帮助(我不是程序员),不幸的是,该帮助是Adobe RoboHelp在框架集中输出的。我想使用下面的第一个函数(getURL)动态构建可在其他函数中重用的URL。例如,在一个函数中将“ a”自变量作为图形传递,或在另一个函数中使用“ 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');
  });


谢谢!

最佳答案

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

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


fullURLgetURL之外不存在。

关于javascript - 在多个功能中访问功能的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17750735/

10-11 22:15
查看更多