JavaScript href在数组中不包含多个单词的条目。

huangapple go评论132阅读模式
英文:

javascript href not including multiword entries in an array

问题

我正在编写一个脚本,该脚本将在网页上以两列的形式显示建议的单词列表。给定的建议单词将显示为超链接,点击后将跳转到另一个页面,显示有关该特定单词的更多信息。我遇到的问题是,超链接只显示数组中建议单词的第一个单词。例如,如果suggestions[m]处的建议单词应该是"dog house",那么超链接只有"dog",因此跳转到错误的页面以获取更多信息。显示的单词是正确的,所以页面上显示的是"dog house",只是超链接不正确。我在href中做错了什么?是否有关于模板的限制,不能使用多个单词的字符串?

if(suggestions.length) {

    var table = '<table cellspacing="1" cellpadding="5">';
    var tolength = suggestions.length
    if (tolength ===1){
        for (var i = 0; i < suggestions.length; i++) {
             var b = suggestions[i]
             table += '<tr>';
             table += '<td><a href=word?word=>' + suggestions[i] + '</a></td>';
             table += '</tr>';
        }
     }
     if(tolength > 1){
        for (var m = 0; m < tolength; m+=2) {
             table += '<tr>';
             table += '<td><a href=word?word='+ suggestions[m] +'>' + suggestions[m] + '</a></td>';
             table += '<td><a href=word?word='+ suggestions[m+1] +'>'  + suggestions[m+1] + '</a></td>';
             table += '</tr>';
         }
      }

      table += '</table>';

      document.getElementById('sug').innerHTML = table;


}
英文:

I am writing a script that will show in two columns a list of suggested words on a website page. A given suggested word will display as a hyperlink that will go to another page with more information about that specific word. The problem I am having is that the hyperlink is only showing the first word in the suggested word at that place in the array. For example, if the suggested word at suggestions[m] was supposed to be "dog house", the hyperlink only has "dog" and thus goes to the wrong other page with more information. The displayed word is correct, so it shows "dog house" on the page, it is just the hyperlink that is incorrect. What am I doing wrong with the href? Is there something about the template that prevents multiword strings?

if(suggestions.length) {

    var table = &#39;&lt;table cellspacing=&quot;1&quot; cellpadding=&quot;5&quot;&gt;&#39;;
    var tolength = suggestions.length
    if (tolength ===1){
        for (var i = 0; i &lt; suggestions.length; i++) {
             var b = suggestions[i]
             table += &#39;&lt;tr&gt;&#39;;
             table += &#39;&lt;td&gt;&lt;a href=word?word=&gt;&#39; + suggestions[i] + &#39;&lt;/a&gt;&lt;/td&gt;&#39;;
             table += &#39;&lt;/tr&gt;&#39;;
        }
     }
     if(tolength &gt; 1){
        for (var m = 0; m &lt; tolength; m+=2) {
             table += &#39;&lt;tr&gt;&#39;;
             table += &#39;&lt;td&gt;&lt;a href=word?word=&#39;+ suggestions[m] + &#39;&gt;&#39; + suggestions[m] + &#39;&lt;/a&gt;&lt;/td&gt;&#39;;
             table += &#39;&lt;td&gt;&lt;a href=word?word=&#39;+ suggestions[m+1] +&#39;&gt;&#39;  + suggestions[m+1] + &#39;&lt;/a&gt;&lt;/td&gt;&#39;;
             table += &#39;&lt;/tr&gt;&#39;
         }
      }

      table += &#39;&lt;/table&gt;&#39;;

      document.getElementById(&#39;sug&#39;).innerHTML = table;


}

答案1

得分: 0

你的问题是字符串中的空格需要编码为%20。将你的建议[m]用encodeURIComponent()进行包装,这将得到一个类似于"dog%20house"的字符串。

table += '

<a href=word?word=' + encodeURIComponent(suggestions[m]) + '>' + suggestions[m] + '

';

你可以参考这个链接了解更多信息:https://www.w3schools.com/tags/ref_urlencode.ASP

英文:

your issue is the spaces in the string need to be encoded to %20 . Wrap your suggestions[m] in encodeURIComponent() this will result in a string like "dog%20house"

table += &#39;&lt;td&gt;&lt;a href=word?word=&#39;+ encodeURIComponent(suggestions[m]) + &#39;&gt;&#39; + suggestions[m] + &#39;&lt;/a&gt;&lt;/td&gt;&#39;;

https://www.w3schools.com/tags/ref_urlencode.ASP

答案2

得分: 0

你缺少了双引号,请尝试使用下面的代码:

table += '<td><a href="word?word=' + suggestions[m] + '">' + suggestions[m] + '</a></td>';
英文:

You missing double quotes, try this code below

table += &#39;&lt;td&gt;&lt;a href=&quot;word?word=&#39;+ suggestions[m] + &#39;&quot;&gt;&#39; + suggestions[m] + &#39;&lt;/a&gt;&lt;/td&gt;&#39;;

huangapple
  • 本文由 发表于 2023年8月9日 09:45:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定