2013-03-08 2 views
2

jQuery를 사용하지 않고 div의 내용을 변경하려고합니다. ID 또는 클래스로 div를 선택하고 싶습니다. ..jQuery없이 요소 텍스트를 변경 하시겠습니까?

function appendHtml(targetC, htmldata) { 
    var theDiv = document.getElementById(targetC); 
    var newNode = document.createElement('div'); 
    newNode.innerHTML = htmldata; 
    theDiv.appendChild(newNode) 
} 

을하지만 하나의 텍스트를 변경하는 방법을 알아낼 질수

어떤 아이디어 :

필자는 작업을 추가 얻을 관리?

답변

0

단순히 innerHTML

var anyDiv = document.getElementById(targetID); 

html = "content"; 
anydiv.innerHTML(html); 
를 사용하여 DIV의 HTML을 변경 여기
function appendHtml(targetC, htmldata) { 
    var theDiv = document.getElementById(targetC); 
    theDiv.innerHTML = htmldata; 
} 

appendHtml('foo', 'new text'); 

이 바이올린 인

하여 제공하는 기능의 변화로 :

function changeHtml(targetC, htmldata) { 
    var theDiv = document.getElementById(targetC); 
    theDIV.innerHTML = htmldata; 

}

3

기본적인 샘플 여기

<html> 
<head> 
<script> 
    function bold(targetC) { 
     var theDiv = document.getElementById(targetC); 
     theDiv.innerHTML = '<b>' + theDiv.innerHTML + '</b>'; 
    } 
</script> 
</head> 

<body onload='bold("message")'> 
    <div>Hello, World!</div> 
    <div id="message">What a nice day!</div> 
</body> 
</html> 
0

에 대한 this 바이올린을 볼은 귀하의 질문에 대답 수있는 fiddle이다. 내가 변화 모두에 전달 그 전에 요소를 얻고 있었다.

function appendHtml(targetC, htmldata) { 
var newNode = document.createElement('div'); 
newNode.innerHTML = htmldata; 
targetC.appendChild(newNode); 
} 

var getDiv = document.getElementById("testing"); 
난 그냥, @Alosyius 그것에게 ID를 부여 이미 존재하는 DIV
관련 문제