데이터 속성이있는 pre
요소 다음에 div
을 추가하려고합니다. 지금까지, 이것은 내가 무엇을 가지고 :JavaScript - 데이터 요소 뒤에 div 추가
HTML
<pre data-color="blue"></pre>
<pre data-color="blue"></pre>
<pre data-color="blue"></pre>
결과를, 자바 스크립트는이해야 후 :
<pre data-color="blue"></pre>
<div class="blue">Blue content</div>
<pre data-color="blue"></pre>
<div class="blue">Blue content</div>
<pre data-color="blue"></pre>
<div class="blue">Blue content</div>
지금까지
HTML 내 함수는 다음과 같습니다.
함수가 순간에 작동하지 않습니다 그러나자바 스크립트
var blue = document.querySelectorAll("[data-color='blue']");
var insertdiv = document.createElement(div.class = 'blue', .textContent = 'Blue content.');
[].forEach.call(blue) {
this.nextElementSibling.appendChild(insertdiv);
};
. jQuery를 사용하지 마십시오.
사용하려면 * 대해 forEach를 *와 같은 것을 사용하면 :'{] .forEach.call (blue, function (el) {console.log (el.id);});'사용하고있는 일부 브라우저는 호스트 객체를 허용하지 않기 때문에 바람직하지 않습니다. 유치원처럼 대우 받다. 개체. OP에서 구문이 수정 되더라도 함수 내 * this *는 정의되지 않으므로 전역 객체가 기본값이며 함수에 전달 된 매개 변수는 아무 것도 수행되지 않습니다. [* Array.prototype.forEach *] (http : //ecma-international.org/ecma-262/5.1/#sec-15.4.4.18). – RobG