의는 "어떻게 당신에게있다"결과는 다음과 같이 될 수 있도록 그래서, 여기에 무엇을 사용해야합니다 부분이 아직 처리되지 않았습니다. 현재 처리중인 <script>
요소는 문서의 마지막 스크립트 요소이므로 document.scripts[ document.scripts.length - 1 ]
을 사용하여 참조 할 수 있습니다. 그런 다음 부모 노드를 찾아 요소를 추가하십시오.
<div>
HI!
<script>
var spanNode = document.createElement('span');
var textNode = document.createTextNode('there.');
spanNode.appendChild(textNode);
/* this is the currently interpreted <script> element
so you can append a child to its parent.
*/
document.scripts[ document.scripts.length - 1 ].parentNode.appendChild(spanNode);
</script>
How are you?
</div>
http://jsfiddle.net/0LsLq9x7/
편집 : http://jsfiddle.net/0LsLq9x7/1/
<div>
HI!
<script>
(function(scriptElement){
// these variables will be local to this closure
var spanNode = document.createElement('span');
var textNode = document.createTextNode('there.');
spanNode.appendChild(textNode);
scriptElement.parentNode.appendChild(spanNode);
// pass a reference to the script element as an argument to the function
}(document.scripts[ document.scripts.length - 1 ]));
</script>
How are you?
</div>
당신이 원하는 마십시오 .. 스크립트 이전 (이후) 스크립트 태그의 위치에 따라 범위를 추가 할 수있는 방법을 요소를 추가? –