나는 자바 스크립트에서 더 새롭다, 그래서 어쩌면 나의 질문은 당신 중 몇몇에 못을 박는 것처럼 보일 것이다.HTML 요소를 동적으로 만드시겠습니까?
<div id = "popUpWin" style = "width:' + width + 'px; height:' + height + 'px;"></div>;
내 질문은 어떻게 자바 스크립트를 사용하여 동적으로 행을 만드는 것입니다 : 나는 그것은 DIV 요소를 생성이 행의 한?
나는 자바 스크립트에서 더 새롭다, 그래서 어쩌면 나의 질문은 당신 중 몇몇에 못을 박는 것처럼 보일 것이다.HTML 요소를 동적으로 만드시겠습니까?
<div id = "popUpWin" style = "width:' + width + 'px; height:' + height + 'px;"></div>;
내 질문은 어떻게 자바 스크립트를 사용하여 동적으로 행을 만드는 것입니다 : 나는 그것은 DIV 요소를 생성이 행의 한?
이렇게하는 데는 최소한 두 가지 방법이 있습니다. DOM API 기능 중 하나입니다
사용 document.createElement
.
// Create the element
var d = document.createElement('div');
// Set its ID
d.id = "popUpWin";
// Set the style properties on the element's `style` object
d.style.width = width + "px";
d.style.height = height + "px";
... 다음 문서에 이미있는 다른 요소에 appendChild
또는 insertBefore
또는 그 유사 물을 사용하여 문서의 어딘가에 넣으십시오.
기존 요소에 대한 사용 insertAdjacentHTML
예는 바로 기존 요소의 마지막 자식으로 div
을 추가 할 것이라고
theOtherElement.insertAdjacentHTML(
'beforeend',
'<div id = "popUpWin" style = "width:' + width + 'px; height:' + height + 'px;"></div>'
);
(그것은 이미 다른 요소를 방해하지 않고). 그 theOtherElement
내부 다른 요소를 제거하고 그냥 div
으로 대체됩니다
theOtherElement.innerHTML =
'<div id = "popUpWin" style = "width:' + width + 'px; height:' + height + 'px;"></div>';
참고 : 기존 요소에
사용 innerHTML
, 당신은 원한다면 내용을 교체합니다.
대신 특정 문제에 대한 해결책을 물어 보는 대신 DOM API를 공부하는 것이 좋습니다. 그래서 미래의 문제에 대한 해결책도 얻을 수 있습니다. https://developer.mozilla. org/en-US/docs/Web/API/Document_Object_Model – the8472
가능한 복제본 [순수한 JavaScript (jQuery가 아님) 만 사용하여 일반 텍스트 HTML로 DOM에 요소 추가] (http://stackoverflow.com/questions/10309650/add -elements-to-the-dom-given-plain-text-html-using-only-pure-javascript-no-jqu) – Jonast92