简体   繁体   English

HTML代码分配给Javascript DOM对象

[英]HTML code assign to a Javascript DOM Object

My ultimate goal is to create a Word DOCX file from a generated HTML page. 我的最终目标是从生成的HTML页面创建Word DOCX文件。 To do this, I used information from the question here from HTML <figure> and <figcaption> to Microsoft Word in which one of the answers mentioned a JavaScript plugin that would convert HTML to DOCX ( jquery plugin by Mark Windsoll which converts HTML to Word). 为此,我使用了来自问题的信息, 从HTML <figure>和<figcaption>到Microsoft Word ,其中一个答案提到了一个JavaScript插件,该插件会将HTML转换为DOCX( Mark Windsoll的jquery插件 ,将HTML转换为Word )。

The example in that answer and the plugin was used as a basis for the following code segment. 该答案和插件中的示例被用作以下代码段的基础。 In my program, I have previously created an HTML page (in a WordPress loop) (not just elements of the page, but an entire properly formatted HTML page) that contains the content I want to convert to DOCX. 在我的程序中,我以前创建了一个HTML页面(在WordPress循环中)(不仅是页面元素,而是整个格式正确的HTML页面),其中包含我想要转换为DOCX的内容。 Also, I have loaded the needed JS and CSS needed by the "HTML to Word plugin". 另外,我已经加载了“ HTML to Word插件”所需的JS和CSS。

The intent is to take the created HTML ( $post_output ) and use the wordExport() function (the JS that converts HTML to DOCX) to create the DOCX file, which I will then save. 目的是采用创建的HTML( $post_output )并使用wordExport()函数(将HTML转换为DOCX的JS)创建DOCX文件,然后将其保存。

<?php 
    $x = $post_output ;  // $post_output contains an HTML page with doctype/head/body/etc that was generated by the loop
    $dom = new DOMDocument;
    libxml_use_internal_errors(false); // supress errors
    $dom->loadHTML($x, LIBXML_NOERROR); // supress errors
?>
<script type="text/javascript">
         $dom.wordExport();
</script>

The error I am getting is that the $dom object in the script is not available, so the wordExport() function is not called. 我得到的错误是脚本中的$dom对象不可用,因此未调用wordExport()函数。

So I need a way to load the entire HTML string ( $x ) into a JavaScript DOM object so that the wordExport() function (which is the HTML to DOCX converter) will work. 因此,我需要一种将整个HTML字符串( $x )加载到JavaScript DOM对象中的方法,以便wordExport()函数(即HTML到DOCX转换器)可以工作。 (added) the HTML 'string' is not a page in the browser, it's a generated 'HTML page source' assigned to a PHP string variable.) (已添加) HTML“字符串”不是浏览器中的页面,而是分配给PHP字符串变量的生成的“ HTML页面源”。)

Added The HTML string needs to be processed by the Word-to-DOCX jQuery plugin. 新增 HTML字符串需要由Word-to-DOCX jQuery插件处理。 Is this possible? 这可能吗?

Don't be fooled by the fact that both JS and PHP have implementations of the DOM API; 不要被JS和PHP都具有DOM API的实现所迷惑; that doesn't mean that creating a PHP object will be any use to you for running a JS method. 这并不意味着创建PHP对象将对您运行JS方法有用。 Remember that the JS runs in the browser, after all the PHP has finished, and all it sees is the output you've sent to the browser. 请记住,在所有PHP完成之后,JS在浏览器中运行,并且看到的只是您发送到浏览器的输出。

So what you need to do is echo that entire string of HTML, so you can pick it up in the JS code. 因此,您需要做的是echo HTML的整个字符串,以便可以在JS代码中进行选择。 You could make it into a JS string (basically, surround it with ' marks, but using json_encode is a handy way of escaping any quotes etc in the content); 您可以将其放入JS字符串中(基本上用'标记将其括起来,但是使用json_encode是一种转义内容中所有引号等的便捷方法); or you could echo it as hidden content on the page. 或者您可以将其作为页面上的隐藏内容回显。 Then run it through jQuery() to get an appropriate object, and use your library. 然后通过jQuery()运行它以获取适当的对象,并使用您的库。

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

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