4

変更した JavaScript 要素を出力するこのコードを見つけました。document.title<title></title>タグを追加しましたが、通常のテキスト エディターで開くウィンドウには と表示されますuntitled

これは通常、テキストが保存される前の状況です。とにかくタイトルを表示する方法はありますか?

    var element=document.getElementById(element_id);
    var newWin=window.open('','Print-Window','width=400,height=400,top=100,left=100');

    newWin.document.open();
    newWin.document.title = "Readings on PageLinks";
    newWin.document.write('<html><head><title>'+newWin.document.title+'</title></head><body   onload="window.print()">'+element.innerHTML+'</body></html>');
    newWin.document.close();

    setTimeout(function(){ newWin.close(); },10);
4

3 に答える 3

3

実際、元のコードも同様に機能しました(Chrome、他のブラウザではテストしませんでした)

var element_id = "id1";
var element = document.getElementById(element_id);
var newWin = window.open('', 'Print-Window', 'width=400,height=400,top=100,left=100');

newWin.document.open();
newWin.document.title = "Readings on PageLinks";
newWin.document.write('<html><head></head><body onload="window.print()">' + element.innerHTML + '</body></html>');
newWin.document.close();

setTimeout(function() {
    newWin.close();
}, 10);​

JSFiddleで見る

于 2012-05-09T20:00:31.717 に答える
1

その時点でページに要素newWin.document.title = "Readings on PageLinks";がなかったため、割り当てが失敗したと思います。<title>

したがって、newWin.document.titleまだ未定義でした。次に、それを string に連結したため、「未定義」として<title>'+newWin.document.title+'</title>取得されました。toString()

したがって、タイトルを文字列に直接書き込むだけです

newWin.document.write('<html><head><title>Readings on PageLinks</title>...');

Eran Medan がコメントで提案したように。

これは私にとってはうまくいきました。

于 2012-05-09T19:52:22.960 に答える
0

ドキュメントで開かれたエディターの動作である可能性があります。ドキュメントが保存されるまで、エディターのヘッダーには「無題」と表示されます。これは仕様によるものです。

于 2012-05-16T14:34:34.693 に答える