新<风格>元素在 IE 中不起作用
我目前有一个 HTML 页面,其主体背景为灰色。现在我想使用 Javascript 覆盖它并将其更改为白色。我还想更改其他一些元素的填充和边距。我尝试使用innerHTML 属性来完成此任务。
问题是,除了新引入的元素之外,一切都正常,该元素未在 IE7 或 IE8 中应用。但它在 FireFox 中确实有效。
<script>
// if using jQuery
$(document).ready(function() {
document.body.innerHTML = '
<style type="text/css">
body {
background-color: #FFFFFF
!important; }
#todayBirthdays, #weekendBirthdays, #noBirthdays, #todayJubileums, #weekendJubileums {
padding: 0 !important;
margin: 0 !important;
}
</style>
<div style="text-align: left; background-color: #FFFFFF">' +
document.getElementById('WebPartctl00_SPWebPartManager1_g_7118b319_c5b0_4214_a46d_27131866cde3').innerHTML +
'</div>';`
});
</script>
你能建议一下吗?
非常感谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
内有效,尽管某些浏览器可能在其他地方尊重它。如果您想使用脚本更改正文背景或其他属性,请使用 jQuery 中相应的
.css()
方法。The
<style>
tag is only valid inside the<head>
, though some browsers may respect it in other places. If you want to change the body background or other properties with a script, use the appropriate.css()
method in jQuery.为什么不只是
Why not just
请参阅 jQuery 的 CSS 属性以及 addclass 方法。这比你正在做的事情容易得多!
虽然我认为你应该改用 addClass 。
See the CSS property of jQuery and also the addclass method. This is much easier than what you are doing!
Although I think you should be using addClass instead.
附加到
>
和
Demo of appending to the
<body
> and the<head>
如果您坚持添加您所要求的内联样式,请使用以下内容:
这会将您的样式附加到文档的 head 元素。但实际上,如果您要使用 jQuery 或 javascript,更好的方法是更改背景值。
或者
If you are stuck on adding in an inline style which is what you asked then use the following:
This will append your styling to the head element of the document. However in reality the better way to go if you are going to use jQuery or javascript is to just change the values of the background.
OR