javascript - 快速响应未在锚标记 href 中添加 html 属性值
问题描述
我正在使用 Nodejs 脚本发送快速响应。在此,我发送 HTML 文本作为响应并设置href
锚标记的值,但我无法在客户端看到它。虽然我可以innerHTML
在标签上看到它。
正如您在下面的代码行中看到的那样,我专注于这个问题。(以下是我的代码中的两行,其中我面临添加/附加 href 值的问题。)
res.write("<p>("+(counter+1)+") "+"<a href='https://patents.google.com/patent/'"+toString(patent_number)+" target='_blank' > "+patent_number+"</a>"+", score: "+patent_A[i]['score']+"</p>")
res.write("<p>click <a href='"+"data:text/csv;charset=utf-8,'"+encodeURI(csv)+"' target='_blank' download='output.csv' >here</a> to download csv file</p>")
另外,我尝试通过发送下载按钮的脚本来添加事件侦听器,它也不起作用。
res.write("<script> document.getElementById('link').innerHTML = 'Hello World' document.body.onload = function(){ function download_csv() { var hiddenElement = document.createElement('a') ")
res.write(" hiddenElement.href = 'data:text/csv;charset=utf-8,' "+ encodeURI(csv))
res.write(" hiddenElement.target = '_blank' ")
res.write(" hiddenElement.download = 'output.csv' } }")
res.write(" </script>")
请参阅下面的代码完整的 Nodejs 代码和屏幕截图。
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("<body>")
res.write("<center><div style='border-style: solid;width: 600px;margin-top: 10%;text-align: left; padding:10px;'>")
// <p>Following patents constitute the top 20% of the portfolio</p>
if(patent_A.length != 0){
res.write("<p><b>Following patents constitute the top 20% of the portfolio</b></p>")
for(var i=0; i<patent_A.length;i++){
counter = i
patent_number = patent_A[i]['patentNumber']
res.write("<p>("+(counter+1)+") "+"<a href='https://patents.google.com/patent/'"+toString(patent_number)+" target='_blank' > "+patent_number+"</a>"+", score: "+patent_A[i]['score']+"</p>")
}
}
// <p>Following patents constitute the top 21-50% of the portfolio</p>
if(patent_B.length != 0){
res.write("<p><b>Following patents constitute the top 21-50% of the portfolio</b></p>")
for(var i=0; i<patent_B.length;i++){
counter = i
patent_number = patent_B[i]['patentNumber']
res.write("<p>("+(counter+1)+") "+"<a href='https://patents.google.com/patent/'"+toString(patent_number)+" target='_blank' > "+patent_number+"</a>"+", score: "+patent_B[i]['score']+"</p>")
}
}
// Following patents constitute the remaining 51-100% of the portfolio
if(patent_C.length != 0){
res.write("<p><b>Following patents constitute the top 21-50% of the portfolio</b></p>")
for(var i=0; i<patent_C.length;i++){
counter = i
patent_number = patent_C[i]['patentNumber']
res.write("<p>("+(counter+1)+") "+"<a href='https://patents.google.com/patent/'"+toString(patent_number)+" target='_blank' > "+patent_number+"</a>"+", score: "+patent_C[i]['score']+"</p>")
}
}
res.write("<p>click <a href='"+"data:text/csv;charset=utf-8,'"+encodeURI(csv)+"' target='_blank' download='output.csv' >here</a> to download csv file</p>")
res.write("<button onclick=download_csv()>Download CSV</button>")
res.write("</div></center>")
res.write("<script> document.getElementById('link').innerHTML = 'Hello World' document.body.onload = function(){ function download_csv() { var hiddenElement = document.createElement('a') ")
res.write(" hiddenElement.href = 'data:text/csv;charset=utf-8,' "+ encodeURI(csv))
res.write(" hiddenElement.target = '_blank' ")
res.write(" hiddenElement.download = 'output.csv' } }")
res.write(" </script>")
res.end()
解决方案
在你写的两个陈述中,你都放错了小引号。
试试下面:
res.write("<p>("+(counter+1)+") "+"<a href='https://patents.google.com/patent/"+toString(patent_number)+"' target='_blank' > "+patent_number+"</a>"+", score: "+patent_A[i]['score']+"</p>")
res.write("<p>click <a href='"+"data:text/csv;charset=utf-8,"+encodeURI(csv)+"' target='_blank' download='output.csv' >here</a> to download csv file</p>")
此外,为了摆脱处理带有值的完整字符串文字的困难,我建议使用字符串插值。
推荐阅读
- apache-spark - Spark:复制另一个表的聚合列 - withColumn
- json - 解析 JSON 给出 JsResultException
- android - 找不到“反应原生”的调试适配器
- java - firebase.database.DatabaseException:反序列化时需要一个 Map,但得到一个类 java.util.ArrayList
- flutter - AutoCompleteTextField 右文本对齐
- .net - MSB3021 在 Visual Studio 中运行 Service Fabric 应用时无法复制文件找不到部分路径错误
- c# - How to overwrite rows in the database when passing values from Console Application?
- ios - Push new UIViewController with transparency
- maven - Maven build parent before other modules
- java - ClassCastException is caused by casting JobRepositoryFactoryBean to JobRepository when Spring boot starts a batch job