英文:
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 = '<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;
}
答案1
得分: 0
你的问题是字符串中的空格需要被编码为 %20。将你的建议[m]用 encodeURIComponent() 包裹,这将得到一个类似于 ""dog%20house"" 的字符串。
table += '<td><a href=word?word='' + encodeURIComponent(suggestions[m]) + ''>'' + suggestions[m] + '</a></td>';
更多信息请参考: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 += '<td><a href=word?word='+ encodeURIComponent(suggestions[m]) + '>' + suggestions[m] + '</a></td>';
答案2
得分: 0
你缺少了双引号,请尝试以下代码:
table += '<td><a href="word?word=' + suggestions[m] + '">' + suggestions[m] + '</a></td>';
英文:
You missing double quotes, try this code below
table += '<td><a href="word?word='+ suggestions[m] + '">' + suggestions[m] + '</a></td>';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论