简体   繁体   English

从Silverlight添加innerHTML不会应用样式

[英]Adding innerHTML from Silverlight does not get styles applied

I am trying to export some HTML content from Silverlight. 我正在尝试从Silverlight导出一些HTML内容。 I grab a div from the DOM and then change the innerHTML property. 我从DOM中获取了一个div,然后更改了innerHTML属性。 Once the HTML is added the HTML does not pick up any styles from the page. 添加HTML后,HTML不会从页面中拾取任何样式。

Here is the Silverlight code 这是Silverlight代码

HtmlDocument doc = HtmlPage.Document
HtmlElement div = doc.GetElementByID("divContent");
div.SetAttribute("innerHTML", "Some content");

here is the HTML 这是HTML

...
<style>
    contentClass{
       color:red;
       font-family:Arial;
       font-size:32;
       font-weight:bold;
    }
</style>
...
<div id="divContent" class="contentClass">
</div>

I have tried several other ways to accomplish this task. 我尝试了其他几种方法来完成此任务。 I have tried adding children to the div with class names applied. 我尝试将子类添加到div并应用类名。 The only way that seems to work it to write out the styles inline with the text. 似乎可行的唯一方法是写出与文本内联的样式。

Any Help would be much appreciated. 任何帮助将非常感激。

Thanks, 谢谢,

Richard 理查德

Does your CSS work when you put content in it beforehand? 预先放置内容时CSS是否起作用? Also, I would change your CSS style into 另外,我会将您的CSS样式更改为

div.contentClass {
   color: red;
   font-family: Arial;
   font-size: 32;
   font-weight: bold;
}

to target it. 瞄准它。 And it's regular CSS, right? 这是普通的CSS,对吧? Then it should be properly wrapped in <style type="text/css"> ** your css ** </style> 然后应将其正确包装在<style type="text/css"> ** your css ** </style>

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

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