JavaScript中href不包括数组中的多词条目。

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

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 += '&lt;td&gt;&lt;a href=word?word=&#39;' + encodeURIComponent(suggestions[m]) + '&#39;&gt;&#39;' + suggestions[m] + '&lt;/a&gt;&lt;/td&gt;';

更多信息请参考: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-2.html
匿名

发表评论

匿名网友

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

确定