【问题标题】:How to edit an inner html, save it and display the same in a different page如何编辑内部 html,将其保存并在不同页面中显示
【发布时间】:2017-06-29 06:46:44
【问题描述】:

编辑内部 html 并保存。那就是下载它:

<script type="text/javascript" >
            $(document).ready(function () {


             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 = 'tags.txt';

            $('.edit').click(function () {
                $(this).hide();
                $('#content').addClass('editable');
                $('p').attr('contenteditable', 'true');
                $('.save').show();
            });

            $('.save').click(function () {
                $(this).hide();
                $('#content').removeClass('editable');
                $('p').removeAttr('contenteditable');
                $('.edit').show();
                downloadInnerHtml(fileName, 'content', 'text/plain');
            });



        });
    </script>

【问题讨论】:

  • 你的问题是什么?
  • 它只适用于 chrome 而不是 firefox 如何让它在 firefox 中也能正常工作
  • 在 Firefox 中发生了什么?有错误吗?
  • 它不会下载
  • 好的,请看我的回答。

标签: javascript jquery asp.net asp.net-mvc html


【解决方案1】:

我无法解释这种火灾反应,但您可以尝试 JQuery 而不是 DOM

  var link  = ($('<a>').insertAfter($('body')))[0]

编辑:好的,找到了!您只需将其附加到页面正文即可。

注入这个:

document.body.appendChild(link);

在调用Click() 方法之前。

【讨论】:

    猜你喜欢
    • 2021-10-31
    • 2019-01-20
    • 2018-12-21
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    相关资源
    最近更新 更多