0

こんにちは、私はこのhtmlを次のように持っています.javascript関数myFunction内でh1スタイルのcssを継承する方法. クリックすると、デフォルトの 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>
4

1 に答える 1

1

document.write (and writeln) will, if called when the document is in a closed state (which it will be after the DOM is ready) first call document.open and then overwrite the document.

Since the document is overwritten, the stylesheet is destroyed.

したがって、解決策は使用しないことdocument.writelnです。代わりに標準の DOM メソッドを使用してください。その方法の紹介が必要な場合は、W3C がWeb 標準カリキュラムをホストしています。このカリキュラムには、DOM のトラバースと HTML の作成と変更に関する章が含まれています。

于 2013-03-28T15:28:30.700 に答える