Javascript 导出div内容的脚本在Chrome中工作,但赢得了';我不能在Firefox中工作
我有以下脚本将div内容导出为文本: JAVASCRIPTJavascript 导出div内容的脚本在Chrome中工作,但赢得了';我不能在Firefox中工作,javascript,Javascript,我有以下脚本将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" va
<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>
//等待页面先加载
window.onload=函数(){
//获取对页面上链接的引用
//id为“mylink”
var a=document.getElementById(“exportxt”);
//设置单击链接时要运行的代码
//通过将函数分配给“onclick”
a、 onclick=function(){
//你的代码在这里。。。
函数下载InnerHTML(文件名、elId、mimeType){
var elHtml=document.getElementById(elId).innerHTML;
var link=document.createElement('a');
mimeType=mimeType | |“text/plain”;
link.setAttribute('下载',文件名);
link.setAttribute('href','data:'+mimeType+';charset=utf-8',+encodeURIComponent(elHtml));
link.click();
}
var fileName='meucanvas.txt';//如果需要,可以使用.txt扩展名
下载InnerHTML(文件名,'editor','text/plain');
//如果你不想链接到
//将浏览器重定向到其他页面,
//“google.com”在我们这里的例子中,那么
//在该块末尾返回false。
//请注意,这也可以防止事件冒泡,
//这可能是我们想要的,但不会
//总是这样。
返回false;
}
}
HTML
但是,它在Firefox中不起作用。在铬上工作。如何使其跨浏览器兼容
是onclick事件吗?您的问题在窗口中。加载firefox中未触发的函数。尝试改用JQuery$(document).ready()
和其他JQuery函数(用于单击等)。JQuery为您处理这些交叉浏览器的内容
选中此项:
问题不一定是window.onload
您的问题是没有在页面上添加link元素,请尝试以下操作:
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;
}
}
提示:
在某些浏览器中,您可能会使用“下载不起作用”属性(可能是手机)
尝试应用程序/octet流
,例如:
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
谢谢Guillerme Nasimento,它现在起作用了。巴西致以最良好的问候。
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