首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Express响应未在锚标签href中添加html属性值

Express响应未在锚标签href中添加html属性值
EN

Stack Overflow用户
提问于 2021-04-28 13:00:52
回答 1查看 35关注 0票数 0

我正在使用Nodejs脚本发送快速响应。在这个示例中,我发送了HTML文本作为响应,还设置了锚标记的href值,但我无法在客户端看到它。而我可以在标签的innerHTML上看到它。

正如您在下面的代码行中所看到的,我将重点放在这个问题上。(下面是我的代码中的两行代码,我在其中遇到了添加/附加href值的问题。)

代码语言:javascript
复制
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>")
代码语言:javascript
复制
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>")

此外,我试图通过发送下载按钮的脚本来添加事件侦听器,但同样不起作用。

代码语言:javascript
复制
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的完整代码和截图请参见下面的代码。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-28 13:22:14

在你写的两个声明中,你把小引号放错了地方。

请在下面进行尝试:

代码语言:javascript
复制
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>")
代码语言:javascript
复制
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>")

此外,为了摆脱处理带有值的完整字符串文字的困难,我建议使用string interpolation

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67294011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档