简体   繁体   English

如何在javascript中以txt格式保存文件但只保存特定部分/特定元素标签

[英]how to save file in txt format in javascript but only specific part/specific element tag

Hey all i want to save txt format a particular data shown on page.嘿,我想将 txt 格式保存为页面上显示的特定数据。

Variables You Entered:您输入的变量:

People you are going to teach:a你要教的人:a

1 Result They Really Want:b他们真正想要的 1 个结果:b

2 Result They Really Want:c 2 他们真正想要的结果:c

1 Thing They FEAR Most: d他们最害怕的一件事:d

2 Thing They FEAR Most:e 2 他们最害怕的事情:e

Time frame You Can Deliver in:f您可以交付的时间范围:f

I'm going to teach a how to b我要教a如何b

I'm going to teach a how to b in c我要教 a 如何 b in c

I'm going to teach a how to c我要教一个如何c

Please do help me out with this thanks a lot.请帮我解决这个问题,非常感谢。

 function copyToClipboard(element) { var $temp = $("<input>"); $("body").append($temp); $temp.val($(element).text()).select(); document.execCommand("copy"); $temp.remove(); } function myFunction() { ///////////////////////////////////////////////////// document.getElementById("but1").style.display = "block"; document.getElementById("but2").style.display = "block"; document.getElementById("but3").style.display = "block"; document.getElementById("but4").style.display = "block"; var a = document.getElementById("myText1").value; var b = document.getElementById("myText2").value; var c = document.getElementById("myText3").value; var d = document.getElementById("myText4").value; var e = document.getElementById("myText5").value; var f = document.getElementById("myText6").value; ////////////////////////////////////////////////////// var col = "Variables You Entered:"; var one = "lorem1:" + a; var two = "#lorem2:" + b; var three = "#2lorem3:" + c; var four = "#1 lorem4: " + d; var five = "#2 lorem5:" + e; var six = "lorem6:" + f; document.getElementById("col").innerHTML = col; //var onetext = one.concat(a); document.getElementById("one").innerHTML = one; document.getElementById("two").innerHTML = two; document.getElementById("three").innerHTML = three; document.getElementById("four").innerHTML = four; var str1 = "test "; var str2 = " test1 "; var str3 = " test2 "; var str4 = " test3 "; var str5 = " test4"; var str6 = " test5 "; var res1 = str1.concat(a,str2,b); document.getElementById("res1").innerHTML = res1; var res2 = str1.concat(a,str2,b,str3,c); document.getElementById("res2").innerHTML = res2; //////////////////////////////// var res3 = str1.concat(a,str2,c); document.getElementById("res3").innerHTML = res3; var res4 = str1.concat(a,str2,c,str3,f); document.getElementById("res4").innerHTML = res4; ///////////////////////////////////////////// } function saveTextAsFile() { var textToWrite = 'test'; var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'}); var fileNameToSaveAs = document.getElementById("wrapper").value; var downloadLink = document.createElement("wrapper"); downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob); downloadLink.download = fileNameToSaveAs; downloadLink.click(); }
 <!DOCTYPE html> <html> <style type="text/css"> #but1,#but2,#but3,#but4,#but5,#but6,#but7,#but8,#but9,#but10,#but11,#but12,#but13,#but14,#but15,#but16,#but17,#but18,#but19,#but20,#but21,#but22 { display: none; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.10/clipboard.min.js"> <body> lorem ipsum text <label>lorem ipsum text</label><br> <input type="text" placeholder="enter a" id="myText1"><br> lorem ipsum text <!--////////////////////////////////////////////////--> <br> <input type="text" placeholder="enter b" id="myText2"><br> lorem ipsum text <!--////////////////////////////////////////////////--> lorem ipsum text <br><input type="text" placeholder="enter c" id="myText3"><br> lorem ipsum text <!--////////////////////////////////////////////////--> lorem ipsum text <br><input type="text" placeholder="enter d" id="myText4"><br> lorem ipsum text <br><input type="text" placeholder="enter e" id="myText5"><br> lorem ipsum text lorem ipsum text <br><input type="text" placeholder="enter f" id="myText6"><br> lorem ipsum text <br><button onclick="myFunction()">Click me</button> <a href="" id="link" onclick="setupDownloadLink('#wrapper', txt.value)" download="code.txt">Download file</a> <div id="wrapper"> <p id="col"></p> <p id="one"></p> <p id="two"></p> <p id="three"></p> <p id="four"></p> <p id="five"></p> <p id="six"></p> <p id="res1"></p> <button id="but1" onclick="copyToClipboard('#res1')">Copy</button> <p id="res2"></p> <button id="but2" onclick="copyToClipboard('#res2')">Copy</button> <p id="res3"></p> <button id="but3" onclick="copyToClipboard('#res3')">Copy</button> <p id="res4"></p> <button id="but4" onclick="copyToClipboard('#res4')">Copy</button> </div>

I Made some adjusts on your JS code, there was some logic errors... also i edited the <a> tag and changed it and its function... I think it is working much better now.我对你的 JS 代码做了一些调整,有一些逻辑错误......我还编辑了<a>标签并改变了它和它的功能......我认为它现在工作得更好。
please, take a look.请看一下。

 function copyToClipboard(element) { var $temp = $("<input>"); $("body").append($temp); $temp.val($(element).text()).select(); document.execCommand("copy"); $temp.remove(); } function myFunction() { ///////////////////////////////////////////////////// document.getElementById("but1").style.display = "block"; document.getElementById("but2").style.display = "block"; document.getElementById("but3").style.display = "block"; document.getElementById("but4").style.display = "block"; var a = document.getElementById("myText1").value; var b = document.getElementById("myText2").value; var c = document.getElementById("myText3").value; var d = document.getElementById("myText4").value; var e = document.getElementById("myText5").value; var f = document.getElementById("myText6").value; ////////////////////////////////////////////////////// var col = "Variables You Entered:"; var one = "People you are going to teach:" + a; var two = "#1 Result They Really Want:" + b; var three = "#2 Result They Really Want:" + c; var four = "#1 Thing They FEAR Most: " + d; var five = "#2 Thing They FEAR Most:" + e; var six = "Time frame You Can Deliver in:" + f; document.getElementById("col").innerHTML = col; //var onetext = one.concat(a); document.getElementById("one").innerHTML = one; document.getElementById("two").innerHTML = two; document.getElementById("three").innerHTML = three; document.getElementById("four").innerHTML = four; var str1 = "I'm going to teach "; var str2 = " how to "; var str3 = " in "; var str4 = " and "; var str5 = " without "; var str6 = " or "; var res1 = str1.concat(a,str2,b); document.getElementById("res1").innerHTML = res1; var res2 = str1.concat(a,str2,b,str3,c); document.getElementById("res2").innerHTML = res2; //////////////////////////////// var res3 = str1.concat(a,str2,c); document.getElementById("res3").innerHTML = res3; var res4 = str1.concat(a,str2,c,str3,f); document.getElementById("res4").innerHTML = res4; ///////////////////////////////////////////// } function saveTextAsFile() { var fileNameToSaveAs = document.getElementById("wrapper").innerText; //Use replace if you want to remove something, like the word Copy... fileNameToSaveAs = fileNameToSaveAs.replace(/Copy/g,""); var textFileAsBlob = new Blob([fileNameToSaveAs], {type:'text/plain'}); var downloadLink = document.createElement("a"); downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob); downloadLink.download = textFileAsBlob; downloadLink.click(); }
 <!DOCTYPE html> <html> <style type="text/css"> #but1,#but2,#but3,#but4,#but5,#but6,#but7,#but8,#but9,#but10,#but11,#but12,#but13,#but14,#but15,#but16,#but17,#but18,#but19,#but20,#but21,#but22 { display: none; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.10/clipboard.min.js"> <body> lorem ipsum text <label>lorem ipsum text</label><br> <input type="text" placeholder="enter a" id="myText1"><br> lorem ipsum text <!--////////////////////////////////////////////////--> <br> <input type="text" placeholder="enter b" id="myText2"><br> lorem ipsum text <!--////////////////////////////////////////////////--> lorem ipsum text <br><input type="text" placeholder="enter c" id="myText3"><br> lorem ipsum text <!--////////////////////////////////////////////////--> lorem ipsum text <br><input type="text" placeholder="enter d" id="myText4"><br> lorem ipsum text <br><input type="text" placeholder="enter e" id="myText5"><br> lorem ipsum text lorem ipsum text <br><input type="text" placeholder="enter f" id="myText6"><br> lorem ipsum text <br><button onclick="myFunction()">Click me</button> <a href="" id="link" onclick="saveTextAsFile()">Download file</a> <div id="wrapper"> <p id="col"></p> <p id="one"></p> <p id="two"></p> <p id="three"></p> <p id="four"></p> <p id="five"></p> <p id="six"></p> <p id="res1"></p> <button id="but1" onclick="copyToClipboard('#res1')">Copy</button> <p id="res2"></p> <button id="but2" onclick="copyToClipboard('#res2')">Copy</button> <p id="res3"></p> <button id="but3" onclick="copyToClipboard('#res3')">Copy</button> <p id="res4"></p> <button id="but4" onclick="copyToClipboard('#res4')">Copy</button> </div>

EDIT : to remove the word "Copy", use the replace method (I updated the snippet and commented that part) with the g tag to remove all occurences...编辑:要删除“复制”一词,请使用带有g标记的替换方法(我更新了代码段并注释了该部分)以删除所有出现的...
about the no line breaks, for me, there is a line break after every line.. if you want to add more, just append some <br> tags and then when will make the download, replace it with \\n\\r关于没有换行符,对我来说,每一行后面都有一个换行符..如果你想添加更多,只需附加一些<br>标签,然后什么时候进行下载,用\\n\\r替换

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript将页面的特定部分保存为图像 - how to save specific part of page as image using JavaScript 是否可以使用javascript在特定目录中保存诸如.swf,.mp3,.txt等文件,而无需“另存为”提示? - Is it possible to save a file such as .swf, .mp3, .txt, etc. without the “save as” prompt using javascript in a specific directory? 更改 JavaScript 中元素字符串特定部分的颜色 - Change color of specific part of element string in JavaScript 使用正则表达式提取JavaScript中的特定标签格式 - Extract specific tag format in javascript using regex 如何在JavaScript中的特定HTML标签下创建元素 - How to create an element in JavaScript, under a specific HTML tag 使用JavaScript正则表达式风格,如何仅在另一个特定标签内时才匹配特定标签? - Using the JavaScript regular expression flavour, how to match a specific tag only when inside another specific tag? 仅使用特定div中的javascript更改链接的一部分 - change part of a link with javascript in a specific div only Javascript:Append 元素到 Class 仅在特定的.html 文件上 - Javascript: Append Element to a Class Only on Specific .html File 在文本文件Javascript的特定部分添加字符串 - Add string in a specific part of text file Javascript 如何在javascript中使用仅在特定位置的元素创建数组? - How to create an array in javascript with element at specific position only?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM