Development Tip

새 창을 열고 jQuery를 사용하여 html을 삽입하는 방법은 무엇입니까?

yourdevel 2020. 11. 12. 20:26
반응형

새 창을 열고 jQuery를 사용하여 html을 삽입하는 방법은 무엇입니까?


자바 스크립트에서 새 창을 열려고하는데 HTML에 아무것도 삽입되지 않습니다.

var callScriptText = $('#callScriptText').html();
var url = '/Action/CallScript/?callScript=';

// Open the current call script in a new window
var openWindow = window.open(url, 'callScriptPopup', 'width = 500, height = 500');
$(openWindow).html(callScriptText);

이유를 아는 사람이 있습니까?


다음은 jQuery를 사용하여 콘텐츠가있는 새 창을 여는 예입니다.

<script>
function nWin() {
  var w = window.open();
  var html = $("#toNewWindow").html();

    $(w.document.body).html(html);
}

$(function() {
    $("a#print").click(nWin);
});​
</script>

<div id="toNewWindow">
    <p>Your content here</p>
</div>

<a href="javascript:;" id="print">Open</a>​

편집 :이 코드가 작동하지 않는다고 말하는 사람들을 위해 여기에 jsfiddle이 있습니다 http://jsfiddle.net/8dXvt/


이 시도:

var x=window.open();
x.document.open();
x.document.write('content');
x.document.close();

Chrome과 IE에서 작동합니다.


@Emre의 답변을 기반으로 구축.

자바 스크립트를 사용하면 연결할 수 있으므로 코드를 다음과 같이 수정했습니다.

var x=window.open();
x.document.open().write('content');
x.close();

또한 새 창 (새 탭이 아님)으로 강제 실행하려면 첫 번째 선 치수를 지정하십시오. 그러나 그것은 세 번째 주장이어야합니다. 따라서 첫 번째 줄을 다음과 같이 변경하십시오.

var x=window.open('','','width=600, height=600');

시험:

var x = window.open('', '', 'location=no,toolbar=0');
x.document.body.innerHTML = 'content';

참고 URL : https://stackoverflow.com/questions/9399354/how-to-open-a-new-window-and-insert-html-into-it-using-jquery

반응형