2017-12-08 25 views
2

현재 나는 내 index.html 파일어떻게

document.title = document.getElementById('title').contentWindow.document.title; 

에 다음 한 index.html을 설정하는 자식은 iframe에서 을받을 수 있나요하지만 제목을 비워 대신 iframe 대응의 제목을합니다.</p>

어떻게 수정합니까?

Here is my site

+0

어떤 요소가 'title'입니까? GetElementById는 ID가있는 DOM 요소를 가져 오는 데 사용됩니다. –

+0

제목이 있어야하는 iframe의 제목은 "title"입니다. – DeepMotions

+0

코드가 작동합니다. 아마도 프레임이로드되기 전에 실행 중일 것입니다. 참조 : https://stackoverflow.com/questions/29233928/iframe-onload-javascript-event –

답변

1

같은 iframetitle만큼 쉽게 얻기 : 당신이 iframe가로드 title의 값을 얻고 싶다면

var iframe = document.getElementById("iframeId"); 
var iframeTitle = iframe.contentDocument.title; 

이 실행 :

document.getElementById("iframeId").onload = function() { 
    var iframeTitle = document.getElementById("iframeId").contentDocument.title; 
} 

title 값이 iframeTitle에 저장되었습니다. 변하기 쉬운.

그런 다음 현재 페이지의 제목 값을이 변수로 바꿀 수 있습니다.

if (document.title != iframeTitle) { 
    document.title = iframeTitle; 
} 

당신이 당신의 html 파일에이 코드를 삽입하여 <body> 요소에 콘텐츠 뒤에 다음 코드를 추가합니다.

<script> 
    document.getElementById("iframeId").onload = function() { 
     var iframeTitle = document.getElementById("iframeId").contentDocument.title; 
     if (document.title != iframeTitle) { 
     document.title = iframeTitle; 
     } 
    } 
</script> 
+0

나는 자바 스크립트가 좋지 않아 코드의 모든 부분을 어디에 넣어야합니까? – DeepMotions

+0

@DeepMotions 위의 최신 코드 스 니펫을 참조하십시오. 닫는 ''태그 앞에 파일의 하단에이 코드를 포함하십시오. 내가 너를 도울 수 있다면 너는 내 대답을 업 그레 이드하고 받아 들인 대답으로 표시하는 것이 좋을 것이다. – Felix

+0

불행히도 이것도 작동하지 않았다 : ( – DeepMotions