javascript附加到文本文件忽略元素

时间:2017-02-11 08:34:40

标签: javascript jquery

我从几个来源拼凑了这个脚本

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">

  <script type="text/javascript" src="http://code.jquery.com/jquery-

git.js"></script>


<script type='text/javascript'>//<![CDATA[
$(function(){
$('.button').click(function(event) {
    event.preventDefault();
    if ($("#TextToSave").text() == "type your text here ...") {
   $("#TextToSave").text($(this).html());
    } else {
        $("#TextToSave").text($("#TextToSave").text() + $(this).html());
    }
});

});//]]> 

</script>


</head>

<body>
  <a href="#" class="button">
<track><location>path 1</location></track></a><br>

<a href="#" class="button">
<track><location>path 2</location></track></a><br>

<a href="#" class="button">
<track><location>path 3</location></track></a> <br>


    <textarea id="TextToSave" cols="48" id="textarea" rows="5" 

onfocus="if(this.value=='type your text here ...')this.value='';" 

onblur="if(this.value=='')this.value='type your text here ...';">type 

your text here ...</textarea>

  <script>
  // tell the embed parent frame the height of the content
  if (window.parent && window.parent.parent){
    window.parent.parent.postMessage(["resultsFrame", {
      height: document.body.getBoundingClientRect().height,
      slug: "BHCdJ"
    }], "*")
  }
</script>

<table>
    <tr>
        <td>Filename to Save As:</td>
        <td><input id="inputFileNameToSaveAs"></input></td>
        <td><button onclick="saveTextAsFile()">Save Text to 

File</button></td>
    </tr>
</table>

<script type="text/javascript">

function saveTextAsFile()
{
    var textToSave = document.getElementById("TextToSave").value;
    var textToSaveAsBlob = new Blob([textToSave], {type:"text/plain"});
    var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
    var fileNameToSaveAs = document.getElementById

("inputFileNameToSaveAs").value;

    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    downloadLink.innerHTML = "Download File";
    downloadLink.href = textToSaveAsURL;
    downloadLink.onclick = destroyClickedElement;
    downloadLink.style.display = "none";
    document.body.appendChild(downloadLink);

    downloadLink.click();
}

function destroyClickedElement(event)
{
    document.body.removeChild(event.target);
}
</script>

</body>
</html>'

我遇到的问题是链接的格式为<a href="#" class="button"> <track><location>path 1</location></track></a><br> 文本复制正常,并创建一个文本文件,其中包含所有文本,但忽略</track>是否有一种简单的方法可以解决此问题?

2 个答案:

答案 0 :(得分:1)

我检查你的代码,但找不到为什么它不起作用,但我为你做了一个简单的代码..希望它在这种情况下适合你

<script type='text/javascript'>
  $(function(){
  $('.button').click(function( ) {
   event.preventDefault();
    if ($("#TextToSave").text() == "type your text here ...") {
      $("#TextToSave").text($(this).html());
    } else {
     $("#TextToSave").text($("#TextToSave").text() + $(this).html()+'</track>');
    }
  });
 });

 </script>

答案 1 :(得分:0)

看起来它可能是JQuery的.html()方法和<track>标记的错误。当我将<track>更改为<p>时,代码运行正常。基于该代码段,您似乎不需要使用<track>标记,是吗?