英文:
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 += '
';
你可以参考这个链接了解更多信息: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>';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论