iframeReference.contentDocument.title = 'some title'
으로 iframe의 실제 제목을 변경할 수 있습니다. h1
태그와 같은 HTML 제목을 변경하려면 해당 참조를 가져 와서 textContent
을 설정하면됩니다. 아래를 참조하십시오.
샘플 마크 업 :
<button id="myBtn">Change Iframe</button>
<h1 id="h1Title">Iframe Title</h1>
<iframe id="myIframe"></iframe>
샘플 자바 스크립트 :
var myBtn = document.getElementById('myBtn');
var myIframe = document.getElementById('myIframe');
var h1Title = document.getElementById('h1Title');
myBtn.addEventListener('click', changeIframe);
function changeIframe() {
myIframe.contentDocument.title = 'New title!';
h1Title.textContent = 'New Title!';
}
당신은 지금 당신의 코드를 사용하여 질문을 업데이트 한 것처럼 Live demo (click).
, 더있다 말하다.
먼저 인라인 js (HTML 요소의 JavaScript)가 잘못되었습니다. 다음 결과 중 일부를 읽어보십시오. https://www.google.com/search?q=Why+is+inline+js+bad%3F
대신 예제를 따라 요소 참조를 받고 이벤트 리스너를 첨부하십시오. 원하는 경우 요소에 데이터를 저장하고 여기에서 데이터를 가져올 수 있습니다.
Live demo (click).
마크 업 :
<div class="links">
<a data-src="a/video" data-title="A video!">Click to Play: A video!</a>
<a data-src="some/title" data-title="Some Title!">Click to Play: Some Title!</a>
<a data-src="another/title" data-title="Another Title!">Click to Play: Another Title!</a>
</div>
<h1 id="h1Title">Iframe Title</h1>
<iframe id="myIframe"></iframe>
자바 스크립트 :
var myIframe = document.getElementById('myIframe');
var h1Title = document.getElementById('h1Title');
var links = document.querySelectorAll('.links a');
for (var i=0; i<links.length; ++i) {
addClickFunc(links[i], i);
}
function addClickFunc(elem, i) {
elem.addEventListener('click', function() {
var title = elem.getAttribute('data-title');
var src = elem.getAttribute('data-src');
changeIframe(title, src);
});
}
function changeIframe(title, src) {
myIframe.src = src;
myIframe.contentDocument.title = title;
h1Title.textContent = title;
}
출처
2014-01-14 17:09:19
m59
당신이 "제목"무엇을 의미합니까? html이란 무엇입니까? – m59
'iframe'에는 title 속성이 없습니다. 당신이 찾고 있는게 뭐지? –
@BrianS 예, 그렇습니다. 그러나 관련성이없는 것으로 보입니다 ... – m59