本文介绍了从函数参数寻址数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我急需一些我认为是小问题的帮助 ,我无法解决。 我正在写一个页面,鼠标悬停在一个链接上,一个图像需要显示,一些文本在数组中定义,应该 出现在表格单元格内(其id为tdDesc)。 这基本上就是HTML文件中的内容: < body onload =" resetDesc(''WS_Standard'',''3'');"> < a href = "#" onmouseover =" showDesc(''WS_Standard'',''1'');" onmouseover =" showDesc(''WS_Standard'',''1'');" > 链接号码1< / a> 这里是'js-file中的'。 01函数showDesc(typeDesc,noDesc ){ 02 var imgDesc =(''img''+ typeDesc +''_''+ noDesc); 03 document.getElementById(''tdDesc''' ).innerHTML = WS_Standard [noDesc]; 04 document.getElementById(imgDesc).style.display ="" ;; 05} 06函数hideDesc(typeDesc,noDesc){ 07 var imgDesc =(''img''+ typeDesc +''_''+ noDesc); 08 document.getElementById(''tdDesc'')。innerHTML = WS_Standard [0]; 09 document.getElementById(imgDesc).style.display =" none" ; 10} 11 function resetDesc(typeDesc,totalDesc){ 12 for(var i = 1; i< = totalDesc; i ++){ 13 var imgDesc =(''img''+ typeDesc +''_''+ i); 14 document.getElementById(img Desc).style.display =" none"; 15} 16 document.getElementById(''tdDesc'')。innerHTML = WS_Standard [0]; 17} var WS_Standard = new Array(); 19 WS_Standard [0] ="没有鼠标悬停时的标准文本" ;; 20 WS_Standard [1] =" Sample Text 1"; 21 WS_Standard [2] =" Sample Text 2"; 22 WS_Standard [3] ="示例文本3"; 我的问题出现在解决 数组中的文本,行03,08,16。我想使用我的typeDesc - 参数 地址阵列,但无论我尝试什么,它都是错误或我得到了 string" WS_Standard"或WS_Standard [1]或或类似的东西,但从不在数组WS_Standard [1]中的 文本。 我做错了什么? 我们非常感谢任何帮助! Martin 解决方案 Martin Altschwager< co ** *@altschwager.de>在消息中写道 news:bb ************************** @ posting.google.c om ... onmouseover =" showDesc(''WS_Standard'',''1'');" 01函数showDesc(typeDesc,noDesc) { 03 document.getElementById(''tdDesc'')。innerHTML = WS_Standard [noDesc]; 你要用字符串下标数组:''1'; - SC 在文章< 42 ********** @ mk-nntp-2.news.uk.tiscali.com>中, " Stephen Chalmers" < IG ****** @ lycos.co.uk>写道: Martin Altschwager< co *** @ altschwager.de>在消息中写道新闻:bb ************************** @ posting.google.c om ... onmouseover =" showDesc(''WS_Standard'',''1'');" 01 function showDesc(typeDesc ,noDesc) {blockquote class =post_quotes> 03 document.getElementById(''tdDesc'')。innerHTML = WS_Standard [noDesc]; 你正在使用字符串订阅数组:''1'; 虽然它不标准,但仍应有效因为数字索引 在实际查找之前被转换为字符串值。 Robert In文章< bb ************************** @ posting.google.com>, co *** @ altschwager.de (Martin Altschwager)写道: 任何帮助都非常感谢! 我不相信我发现你的javascript函数 有任何问题。我填写了缺少的html并更改了一个事件处理程序。 这是我修改过的文件版本。 Robert <!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN" " http://www.w3.org/ TR / html4 / loose.dtd"> < html> < head> < title>保存统计数据< / title> < base href =" http://spaceplace.jpl.nasa.gov/en/_images/"> < script type = " text / javascript"> / * 在解决 数组中的文字时出现问题,第03,08,16行。我想使用我的typeDesc - 参数 地址阵列,但无论我尝试什么,它都是一个错误,或者我得到一个 string" WS_Standard"或WS_Standard [1]或或类似的东西,但从来没有数组WS_Standard [1]中的 文本。 更改 调用onmouseout 调用hideDesc 您不显示要更改的文本也不显示图像。可以在id属性中使用 错误的值 这是一个糟糕的变量名称tdDesc''因为它很难辨认 d来自D. * / var WS_Standard = new Array(); WS_Standard [0] =" Standard没有鼠标悬停时的文字; WS_Standard [1] =" Sample Text 1"; WS_Standard [2] =" Sample Text 2" WS_Standard [3] ="示例文本3" 函数showDesc(typeDesc,noDesc){ var imgDesc =(''img'' + typeDesc +''_''+ noDesc); document.getElementById(''tableDescription'')。innerH TML = WS_Standard [noDesc]; document.getElementById(imgDesc).style.display ="" ;; } function hideDesc(typeDesc,noDesc){ var imgDesc =(''img''+ typeDesc +''_''+ noDesc); document.getElementById(''tableDescription'')。innerH TML = WS_Standard [0]; document.getElementById(imgDesc).style.display =" none" ;; } 函数resetDesc(typeDesc,totalDesc){ for(var i = 1;我< totalDesc; i ++){ var imgDesc =(''img''+ typeDesc +''_''+ i); document.getElementById(imgDesc).style.display =" none"; } document.getElementById(''tableDescription'')。innerH TML = WS_Standard [0]; } < / script> < / head> < body onload =" resetDesc(''WS_Standard'',WS_Standard.length);"> < a href ="#" onmouseover =" showDesc(''WS_Standard'',1);" onmouseout =" hideDesc(''WS_Standard'',1);"> 链接号码1< / a> < p> < img id =" imgWS_Standard_1" src =" common / nasa_header / logo_nasa.gif"> < br> < img id =" imgWS_Standard_2" src =" common / nasa_header / logo_nasa.gif"> < br> < img id =" imgWS_Standard_3" src =" common / nasa_header / logo_nasa.gif"> < / p> < p> < span id =" tableDescription" class =" hidden">要显示的消息< / span> < / p> < / body> < ; / html> Hi, I desperately need some help with what I assume to be a minor problemthat I just can''t solve. I''m writing a page where on mouseover a link, an image needs to bedisplayed and some text, which is defined inside an array, souldappear inside a table cell (whose id is "tdDesc"). That''s basically what''s in the HTML-file: <body onload="resetDesc(''WS_Standard'',''3'');"><a href="#" onmouseover="showDesc(''WS_Standard'',''1'');"onmouseover="showDesc(''WS_Standard'',''1'');">Link no. 1</a> Here''s what''s in the js-File. 01 function showDesc(typeDesc,noDesc) {02 var imgDesc = (''img'' + typeDesc + ''_'' + noDesc);03 document.getElementById(''tdDesc'').innerHTML =WS_Standard[noDesc];04 document.getElementById(imgDesc).style.display = "";05 }06 function hideDesc(typeDesc,noDesc) {07 var imgDesc = (''img'' + typeDesc + ''_'' + noDesc);08 document.getElementById(''tdDesc'').innerHTML = WS_Standard[0];09 document.getElementById(imgDesc).style.display = "none";10 }11 function resetDesc(typeDesc,totalDesc) {12 for (var i = 1; i <= totalDesc; i++) {13 var imgDesc = (''img'' + typeDesc + ''_'' + i);14 document.getElementById(imgDesc).style.display = "none";15 }16 document.getElementById(''tdDesc'').innerHTML = WS_Standard[0];17 }18 var WS_Standard = new Array();19 WS_Standard[0] = "Standard text when no mouse over";20 WS_Standard[1] = "Sample Text 1";21 WS_Standard[2] = "Sample Text 2";22 WS_Standard[3] = "Sample Text 3"; My problem appears when it comes to adressing the text that is in thearray, lines 03, 08, 16. I want to use my "typeDesc"-argument toadress the array, but whatever I try, it''s either an error or I get astring "WS_Standard" or "WS_Standard[1]" or something, but never thetext in the array WS_Standard[1]. What am I doing wrong? Any help is highly appreciated! Martin 解决方案 Martin Altschwager <co***@altschwager.de> wrote in messagenews:bb**************************@posting.google.c om... onmouseover="showDesc(''WS_Standard'',''1'');" 01 function showDesc(typeDesc,noDesc){03 document.getElementById(''tdDesc'').innerHTML = WS_Standard[noDesc]; You''re subscripting the array with the string: ''1''; --S.C. In article <42**********@mk-nntp-2.news.uk.tiscali.com>,"Stephen Chalmers" <ig******@lycos.co.uk> wrote: Martin Altschwager <co***@altschwager.de> wrote in message news:bb**************************@posting.google.c om... onmouseover="showDesc(''WS_Standard'',''1'');" 01 function showDesc(typeDesc,noDesc) {03 document.getElementById(''tdDesc'').innerHTML = WS_Standard[noDesc]; You''re subscripting the array with the string: ''1''; While it isn''t standard, that should still work since numeric indexesare converted a string value before the actual lookup. RobertIn article <bb**************************@posting.google.com >, co***@altschwager.de (Martin Altschwager) wrote: Any help is highly appreciated! I do not believe that I found any problems with the javascript functionsyou showed. I filled in the missing html and changed one event handler. Here is my revised version of the file. Robert <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Saving stats</title><base href="http://spaceplace.jpl.nasa.gov/en/_images/"><script type="text/javascript">/*My problem appears when it comes to adressing the text that is in thearray, lines 03, 08, 16. I want to use my "typeDesc"-argument toadress the array, but whatever I try, it''s either an error or I get astring "WS_Standard" or "WS_Standard[1]" or something, but never thetext in the array WS_Standard[1]. changescall onmouseoutcall hideDescyou do not show the text to change nor the images. Could have thewrong values in the id attributesthis is a bad variable name tdDesc ''cause it is hard to distingishthe d from the D. */var WS_Standard = new Array();WS_Standard[0] = "Standard text when no mouse over";WS_Standard[1] = "Sample Text 1";WS_Standard[2] = "Sample Text 2";WS_Standard[3] = "Sample Text 3";function showDesc(typeDesc,noDesc) {var imgDesc = (''img'' + typeDesc + ''_'' + noDesc);document.getElementById(''tableDescription'').innerH TML =WS_Standard[noDesc];document.getElementById(imgDesc).style.display = "";}function hideDesc(typeDesc,noDesc) {var imgDesc = (''img'' + typeDesc + ''_'' + noDesc);document.getElementById(''tableDescription'').innerH TML =WS_Standard[0];document.getElementById(imgDesc).style.display = "none";} function resetDesc(typeDesc,totalDesc) {for (var i = 1; i < totalDesc; i++) {var imgDesc = (''img'' + typeDesc + ''_'' + i);document.getElementById(imgDesc).style.display = "none";}document.getElementById(''tableDescription'').innerH TML =WS_Standard[0];}</script></head><body onload="resetDesc(''WS_Standard'',WS_Standard.length );"><a href="#" onmouseover="showDesc(''WS_Standard'',1);"onmouseout="hideDesc(''WS_Standard'',1);">Link no. 1</a> <p><imgid="imgWS_Standard_1"src="common/nasa_header/logo_nasa.gif"><br><imgid="imgWS_Standard_2"src="common/nasa_header/logo_nasa.gif"><br><imgid="imgWS_Standard_3"src="common/nasa_header/logo_nasa.gif"></p><p><span id="tableDescription" class="hidden">Message to display</span></p></body></html> 这篇关于从函数参数寻址数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 15:27