繁体   English   中英

导出div内容的脚本在Chrome中有效,但在Firefox中不起作用

[英]Script that exports div content works in Chrome, but won't work in Firefox

我有以下脚本将div内容导出为文本:

JAVASCRIPT

  <script type="text/javascript">

      // Wait for the page to load first
        window.onload = function() {

          //Get a reference to the link on the page
          // with an id of "mylink"
          var a = document.getElementById("exportxt");

          //Set code to run when the link is clicked
          // by assigning a function to "onclick"
          a.onclick = function() {

            // Your code here...


function downloadInnerHtml(filename, elId, mimeType) {
    var elHtml = document.getElementById(elId).innerHTML;
    var link = document.createElement('a');
    mimeType = mimeType || 'text/plain';
    link.setAttribute('download', filename);
    link.setAttribute('href', 'data:' + mimeType  +  ';charset=utf-8,' + encodeURIComponent(elHtml));
    link.click(); 
}
var fileName =  'meucanvas.txt'; // You can use the .txt extension if you want
downloadInnerHtml(fileName, 'editor','text/plain');
            //If you don't want the link to actually 
            // redirect the browser to another page,
            // "google.com" in our example here, then
            // return false at the end of this block.
            // Note that this also prevents event bubbling,
            // which is probably what we want here, but won't 
            // always be the case.
            return false;
          }
        }

</script>

HTML

<div style="float:left; padding-left:5px;">
    <a id="exportxt">
    <label>
    <button type="submit" value="#" style="border: 0; background: transparent">
        <b>CLICK HERE DOWNLOAD AS TXT</b>
    </button>
    </label>
    </a>
    </div>




    <div id="editor">
        </br>
    </br>




    TEXT TEXT TEXT

http://jsfiddle.net/JVke4

但是,它不适用于Firefox。 适用于Chrome。 如何使其跨浏览器兼容?

这是onclick事件吗?

问题不一定是window.onload

您的问题是您没有在页面上添加链接元素,请尝试以下方法:

function downloadInnerHtml(filename, elId, mimeType) {
    var elHtml = document.getElementById(elId).innerHTML;
    var link = document.createElement('a');
    mimeType = mimeType || 'text/plain';
    link.setAttribute('download', filename);
    link.setAttribute('href', 'data:' + mimeType  +  ';charset=utf-8,' + encodeURIComponent(elHtml));
    link.style.cssText = "position: aboslute !important; left: -9999px; visibility: hidden;";//hide element
    link.innerHTML = "text";
    document.body.appendChild(link);
    link.click(); 
    setTimeout(function(){
        document.body.removeChild(link);//remove element
    }, 1);
}

// Wait for the page to load first
window.onload = function() {

  //Get a reference to the link on the page
  // with an id of "mylink"
  var a = document.getElementById("exportxt");

  //Set code to run when the link is clicked
  // by assigning a function to "onclick"
  a.onclick = function() {

    // Your code here...
    var fileName =  'meucanvas.txt'; // You can use the .txt extension if you want
    downloadInnerHtml(fileName, 'editor','text/plain');
    //If you don't want the link to actually 
    // redirect the browser to another page,
    // "google.com" in our example here, then
    // return false at the end of this block.
    // Note that this also prevents event bubbling,
    // which is probably what we want here, but won't 
    // always be the case.
    return false;
  }
}

小费:

在某些浏览器中,您可能会使用属性下载不起作用(可能是移动设备)尝试application/octet-stream ,例如:

function downloadInnerHtml(filename, elId, mimeType) {
    var elHtml = document.getElementById(elId).innerHTML;
    var link = document.createElement('a');
    mimeType = mimeType || 'application/octet-stream';
    link.setAttribute('download', filename);
    link.setAttribute('href', 'data:' + mimeType  +  ';charset=utf-8,' + encodeURIComponent(elHtml));
    link.style.cssText = "position: aboslute !important; left: -9999px; visibility: hidden;";//hide element
    link.innerHTML = "text";
    document.body.appendChild(link);
    link.click(); 
    setTimeout(function(){
        document.body.removeChild(link);//remove element
    }, 1);
}
...
downloadInnerHtml('test.txt', 'editor');//force download

你的问题是在window.load函数中,它没有在firefox中触发。 尝试使用JQuery $(document).ready()和其他JQuery函数(用于点击等)。 JQuery为你处理那些crossbrowser的东西。

检查一下:

window.onload功能在Mozilla Firefox上不起作用

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM