2010-05-17 5 views
1
내가 동적 웹 페이지에 자바 스크립트를 사용하여 검색 상자를 추가 할

에 자바 스크립트를 사용하여 검색 상자를 추가동적 웹 페이지

initially 나는이 작업을 수행 할 수있는 방법을 finally

?

답변

1

는이 같은 뭔가를 할 수있는, 자바 스크립트 라이브러리없이이 .. 일을 가야하는 방법에는 여러 가지가 있습니다

var form = document.createElement('form'); 
form.setAttribute('action', '/some_path'); 
form.setAttribute('method', 'post'); 

var text_field = document.createElement('input'); 
text_field.setAttribute('type', 'text'); 
text_field.setAttribute('value', 'enter something here...'); 

var button = document.createElement('input'); 
button.setAttribute('type', 'submit'); 
button.setAttribute('value', 'Go!'); 

form.appendChild(text_field); 
form.appendChild(button); 

document.body.appendChild(form); 

와 자바 스크립트 라이브러리

, 이것은 훨씬 덜 코드가된다. 예를 들어 jQuery의 경우 :

var form  = $('<form>').attr({action: '/some_path', method: 'post'}), 
    text_field = $('<input>').attr({type: 'text', value: 'enter something here...'}), 
    button  = $('<input>').attr({type: 'submit', value: 'Go!'}); 

form.append(text_field); 
form.append(button); 

$('body').append(form);