使用外部css或插入样式标记来记录片段

时间:2017-02-22 14:45:56

标签: javascript css

我有文档片段,我动态创建并发送到我的打印机进行打印。

var docHtml = document.createDocumentFragment();

var container = document.createElement("div");
container.innerHTML = "<div>" +
                            "Some content goes here" +
                      "</div>";

docHtml.appendChild(container);
var docsToPrint = MSApp.getHtmlPrintDocumentSource(docHtml);

1)我有打印边距的问题,我不知道/在哪里放我的CSS规则:

@page { 
    margin:0cm;
}

进入我的文档片段?

2)是否有可能以某种方式在文档片段中使用外部css文件或插入&#39; style&#39;用所有css规则标记成片段?

5 个答案:

答案 0 :(得分:2)

只要您在内部创建def sigmoid(x): return 1 / (1 + np.exp(-x)) def dsigmoid(y): return y * (1.0 - y) class NeuralNetwork: def __init__(self, nb_inputs, nb_hidden, nb_outputs, learning_rate): self.nb_inputs = nb_inputs self.nb_hidden = nb_hidden self.nb_outputs = nb_outputs self.learning_rate = learning_rate self.output_deltas = None self.output_weights = np.random.random((nb_outputs, nb_hidden)) # WRONG: should be between -1 and 1, not 0 and 1 self.outputs = None self.hidden_deltas = None self.hidden_weights = np.random.random((nb_hidden, nb_inputs)) # WRONG: should be between -1 and 1, not 0 and 1 self.hidden_outputs = None def forward_propagation(self, inputs): self.hidden_outputs = np.zeros((self.nb_hidden,)) self.outputs = np.zeros((self.nb_outputs,)) # get outputs for hidden layer for i in range(self.nb_hidden): aggregated = sum([inputs[j] * self.hidden_weights[i][j] for j in range(self.nb_inputs)]) self.hidden_outputs[i] = sigmoid(aggregated) # get inputs for output layer for i in range(self.nb_outputs): aggregated = sum([self.hidden_outputs[j] * self.output_weights[i][j] for j in range(self.nb_hidden)]) self.outputs[i] = sigmoid(aggregated) def backpropagation(self, expected_outputs): # find deltas for output layer for i in range(self.nb_outputs): for j in range(self.nb_hidden): self.output_deltas[i][j] = dsigmoid(self.outputs[i]) * (expected_outputs[i] - self.outputs[i]) # find deltas for hidden layer for i in range(self.nb_hidden): for j in range(self.nb_inputs): total = 0.0 for k in range(self.nb_outputs): total += self.output_deltas[k][i] * self.output_weights[k][i] self.hidden_deltas[i][j] = dsigmoid(self.hidden_outputs[i]) * total # change weights for output layer for i in range(self.nb_outputs): for j in range(self.nb_hidden): self.output_weights[i][j] += self.learning_rate * self.output_deltas[i][j] * self.outputs[i] # WRONG: should be self.hidden_outputs[j] # change weights for inputs layer for i in range(self.nb_hidden): for j in range(self.nb_inputs): self.hidden_weights[i][j] += self.learning_rate * self.hidden_deltas[i][j] * self.hidden_outputs[i] # WRONG: should be inputs[j] def train(self, data, nb_iterations): for i in range(nb_iterations): # Init deltas self.output_deltas = np.zeros((self.nb_outputs, self.nb_hidden), dtype=np.float64) self.hidden_deltas = np.zeros((self.nb_hidden, self.nb_inputs), dtype=np.float64) # Train on examples with different orders for inputs, expected_output in sorted(data.items(), key=lambda x: random.random()): expected_outputs = np.array([expected_output]) self.forward_propagation(inputs) self.backpropagation(expected_outputs) # WRONG: need inputs: backpropagation(inputs, expected_outputs) def predict(self, inputs): self.forward_propagation(inputs) return self.outputs 的HTML页面链接到div文件,这应该不是问题。

试试这个:

css

或更优雅:

container.innerHTML = "<div style='margin:0;'>" +
                            "Some content goes here" +
                      "</div>";

并在你的CSS中:

container.innerHTML = "<div class='nomargin'>" +
                            "Some content goes here" +
                      "</div>";

答案 1 :(得分:1)

您需要动态创建style元素并将其附加到正文。您的css规则应该是此style元素的内部内容。

$("<style type='text/css'> .blue{ background-color:blue;} </style>").appendTo("head");

答案 2 :(得分:1)

好的,试着这样做:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>

        <!-- Inline Print CSS -->
        <style type="text/css" media="print">
            .test {
                background-color: red;
                width:50px;
                height:50px;
            }
        </style>
        <!-- Inline Print CSS -->

        <!-- OR -->

        <!-- External Print CSS -->
        <link rel="stylesheet" href="path/to/file/print.css" media="print" >
        <!-- / External Print CSS -->

    </head>

        <!-- Injected Content by your Js script -->
        <div class="test">

        </div>
        <!-- / Injected Content by your Js script -->

    <body>

    </body>
    </html>

答案 3 :(得分:0)

IMO,只需在css文件(外部文件)中创建所需的规则,并在html头中调用它。然后只需将类(根据您在css文件中创建的类)添加到使用javascript代码生成的div。它更干净,易于使用。正如@Koby Douek所说的那样。 :)

答案 4 :(得分:0)

如果是这样,那么您拥有的最佳选择就是添加内联的CSS。类似的东西:

container.innerHTML = "<div style='width:30px; height:30px; padding:0;'>" +
                        "Some content goes here" +
                  "</div>";