您好,我有这个网页如下,如何继承h1风格的css内部的javascript函数myFunction。单击后,它会选择默认的h1样式。谢谢。
<html>
<style type="text/css">
body {
color: purple;
font: normal 18px Verdana, Arial, sans-serif;
background-color: white }
h1 {
color: Red;
font: normal 20px Verdana, Arial, sans-serif;
background-color: white }
</style>
<body>
<p id="demo"><h1>Click here.</h1></p>
<button onclick="myFunction()">Check it</button>
<script type="text/javascript">
function myFunction()
{
document.writeln("<h1>", "Hello there", "</h1>");
}
</script>
</body>
</html>发布于 2013-03-28 23:28:31
如果在文档处于关闭状态(在DOM准备就绪后)时调用document.write (和writeln),则将首先调用document.open,然后覆盖文档。
由于文档被覆盖,样式表将被销毁。
所以解决方案是不使用document.writeln. 改用标准的DOM方法。如果您需要介绍如何做到这一点,那么W3C提供了一个web standards curriculum,其中包含有关遍历DOM以及创建和修改HTML的章节。
https://stackoverflow.com/questions/15685798
复制相似问题