使用jQuery动态创建html

时间:2017-07-14 22:34:08

标签: javascript jquery html css

我正在尝试根据现有html页面的内容构建一个新的html页面,将整个内容传递给api。

当我尝试做类似的事情时:

var contents = $('#page-section').html();
var fullPage = $.parseHTML('<html><head><link rel="stylesheet" href="newStyle.css"></head ><body></body></html >').appendTo("body", contents); 

内容变量很好,但对于fullPage,我收到一个错误。目标是展示&#34;内容&#34;使用newStyle.css

还有其他方法可以达到这个目的吗?

我想这样做的原因是将字符串传递给pdf生成器。

2 个答案:

答案 0 :(得分:0)

.appendTo()不期望第二个参数。使用contents <body></body>字符串连接HTML。或使用DOMParser()创建document

var contents = $('#page-section').html();
var fullPage = `<!DOCTYPE html>
                  <html>
                    <head>
                      <link rel=stylesheet href=newStyle.css>
                    </head>
                    <body>${contents}</body>
                  </html >`;

// do stuff with `fullPage` `HTML` string

答案 1 :(得分:0)

我最终使用了一件简单的事情:

using (var pbReader = new Google.Protobuf.CodedInputStream(clientStream, true))
{
    var inRequest = new ProtoMsgStructRequest();
    pbReader.ReadMessage(inRequest);
    {
        var outResponse = new ProtoMsgStructResponse();
        using (var pbWriter = new Google.Protobuf.CodedOutputStream(clientStream, true))
        {
            pbWriter.WriteMessage(outResponse);
        }
    }
}