アプリ版:「スタンプのみでお礼する」機能のリリースについて

任意のHTMLファイルの<body>内に
<script type="text/javascript" src="sample.js" charset="shift_jis"></script>
と書いたとき、そのHTMLファイルのヘッダに
sample.cssへのlinkを定義できるように

--------- sample.js -----------
sample_css=document.createElement('link');
sample_css.rel='stylesheet';
sample_css.href='/sample.css';
sample_css.type='text/css';
document.lastChild.firstChild.appendChild(sample_css);

--------- sample.js -----------

と書いたのですが、IE5.5で、
document.lastChild.firstChild.appendChild(sample_css);
の行がエラーとなってしまいます。
IE5.5に対応できる書き方はないものでしょうか。

A 回答 (1件)

古いIEの環境がないのであてずっぽになりますが・・・



sample_css=document.createElement('link');
sample_css.setAttribute('rel','stylesheet');
sample_css.setAttribute('href','/sample.css');
sample_css.setAttribute('type','text/css');
document.getElementsByTagName("head")[0].appendChild(sample_css);

とかでどうでしょう?
    • good
    • 0
この回答へのお礼

ありがとうございました。IE5.5~7.0、FireFox、Opera、Safariとも全て動きました。

お礼日時:2008/06/24 14:16

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!