【html转word案例】

function getTextContent(element) {
        let text = "";
        const childNodes = element.childNodes;
        for (let i = 0; i < childNodes.length; i++) {
          const node = childNodes[i];
          if (node.nodeType === Node.TEXT_NODE) {
            text += node.textContent;
          } else if (node.nodeType === Node.ELEMENT_NODE) {
            text += getTextContent(node);
          }
        }
        if (text == "删除试题" || text == "编辑试题 " || text.includes("|")) {
          return "";
        }
        return text;
      }
      window.onload = function () {
        const content = document.getElementById("form1");
        const downloadBtn = document.getElementById("downloadBtn");
        downloadBtn.addEventListener("click", () => {
          let indexsum=0
          const textContent = getTextContent(content);
          let textvalue
          textvalue = textContent.replace(/\s+/g, "");

          textvalue=textvalue.replace(/([0-9]{1,3}\.){2,3}/g, (match) => {
             let title=match.split('.')
            return "<br>//题目 " + title[0] + "<br>";
          });
          
          textvalue= textvalue.replace(/([A-Z]{1,}:)/g, (match) => {
            return '<br>['+match.slice(0,1)+'].';
          });
          textvalue = textvalue.replace(/:/g, "");
          textvalue= textvalue.replace(/单选题|多选题|填空题|判断题/g, (match) => {
            indexsum+=1
            return '//段落'+indexsum+'<br>'+match;
          });
          textvalue= textvalue.replace(/参考答案:/g, (match) => {
            return `<br>[答案]`;
          });
          textvalue= textvalue.replace(/详细解答:/g, (match) => {
            return `<br>[题解]`;
          });
          textvalue= textvalue.replace(/分值/g, (match) => {
            return `<br>[分值]`;
          });
          textvalue= textvalue.replace(/分<br>/g, (match) => {
            return `<br>`;
          });

          // const html = content.innerHTML;
          const converted = htmlDocx.asBlob(textvalue);
          const url = window.URL.createObjectURL(converted);
          const link = document.createElement("a");
          link.href = url;
          link.download = "试卷.doc";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          window.URL.revokeObjectURL(url);
        });
      };
          /**
           * @desc 从URL取得base64
           * @param {url} String 网址
           * @param {callback} Function 如果能打开图片,参数为打开图片的blob,如果不能打开图            片,参数为null
           * @return void
           */
function getBase64(url,callback) {
    const xhr = new XMLHttpRequest();
    xhr.responseType='blob'
    xhr.onload = () => {
        const blob=xhr.response;
        var reader = new FileReader();
        reader.onload = function (e) {
          callback(reader.result);
        }
        reader.readAsDataURL(blob);
    };
    xhr.onerror = (err) => {
          callback(null)
        };
        xhr.open('GET', url, true);
        xhr.send(null);
}
 let img=images[i];
 let url=img.src;
 getBase64(url,function(base64){
   if(base64==null){
     img.remove();
     _iterate(i);
   }else {
      img.src=base64;
      _iterate(++i);
   }
})

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不听念经

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值