首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >javascript函数中的<h1> </h1>属性

javascript函数中的<h1> </h1>属性
EN

Stack Overflow用户
提问于 2013-03-28 23:26:32
回答 1查看 202关注 0票数 0

您好,我有这个网页如下,如何继承h1风格的css内部的javascript函数myFunction。单击后,它会选择默认的h1样式。谢谢。

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2013-03-28 23:28:31

如果在文档处于关闭状态(在DOM准备就绪后)时调用document.write (和writeln),则将首先调用document.open,然后覆盖文档。

由于文档被覆盖,样式表将被销毁。

所以解决方案是不使用document.writeln. 改用标准的DOM方法。如果您需要介绍如何做到这一点,那么W3C提供了一个web standards curriculum,其中包含有关遍历DOM以及创建和修改HTML的章节。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15685798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档