如何使HTML下载变量作为文本?

时间:2019-01-26 08:04:28

标签: html css

Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(Criteria.where("creationdate").gte(date1).lte(date2).andOperator(Criteria.where("completiondate").ne(""))),
                Aggregation.project("servicerequesttype").and(DateOperators.DateFromString.fromStringOf("completiondate").withFormat("%Y-%m-%d")).minus(DateOperators.DateFromString.fromStringOf("creationdate").withFormat("%Y-%m-%d")).as("diff"),
                Aggregation.group("servicerequesttype").avg("diff").as("average")
        );

您能帮我下载

<style>
body {background-color: black;}
p  {
  color: white;
  font-family: courier;
  font-size: 160%;
}
</style>
<p >text</p>

作为文本文件?

1 个答案:

答案 0 :(得分:0)

使用以下功能,您可以下载<p>的文本

function handleToTxt(text){
    var element = document.createElement("a");   
    const file = new Blob([text], {type: 'text/plain'});
    element.href = URL.createObjectURL(file);
    element.download = `yourFilename.txt`;
    element.click();
}
<style>
body {background-color: black;}
p {
  color: white;
  font-family: courier;
  font-size: 160%;
}
</style>
<p id="myText">text</p>
<button style="color: black;" onclick="handleToTxt(document.getElementById('myText').textContent)">download </button>