이 번역은 완료되지 않았습니다. 이 문서를 번역해 주세요.

"button"  타입을 가지는 <input> 요소는 간단한 푸시 버튼으로 렌더링 됩니다. 이벤트 핸들러 기능을 할당하여 웹페이지의 어느곳에서나 사용자 지정 기능을 제어하도록 프로그래밍할 수 있습니다(특히 click 이벤트).

Note:  <input> 요소에 "button" 타입을 넣는 방식은 완벽히 올바른 방법이지만, 새로운 <button> 요소를 사용하는 것이 최근 버튼을 생성할때 선호되는 방식입니다. <button>의  label text가 시작태그와 종료태그 사이에 삽입되어 있는 경우, label내에 HTML을 포함할 수 있습니다. 심지어 이미지도 가능합니다.

Value A DOMString used as the button's label
Events click
Supported common attributes type, and value
IDL attributes value
Methods None

Value

input type="button"> 요소의 value 속성은 버튼의 label로 사용되는 DOMString을 담고있습니다.

<input type="button" value="Click Me">

value 를 특정하지 않는다면, 비어있는 버튼을 생성합니다.

<input type="button">

Using buttons

<input type="button"> 요소는 디폴트 기능을 가지지 않습니다(<input type="submit"> 와 <input type="reset"> 는 각각 폼을 제출, 초기화하는 디폴트 기능을 가집니다). 버튼으로 무언가를 하기 위해서는 자바스크립트 코드를 작성해야 합니다.

간단한 버튼

click 이벤트 핸들러를 통해 간단한 버튼을 생성하여 machine을 작동시켜 봅시다. (버튼의 value 와 다음 단락의 내용을 전환합니다.):

<form> 
  <input type="button" value="Start machine">
</form>
<p>The machine is stopped.</p>
var btn = document.querySelector('input');
var txt = document.querySelector('p');

btn.addEventListener('click', updateBtn);

function updateBtn() {
  if (btn.value === 'Start machine') {
    btn.value = 'Stop machine';
    txt.textContent = 'The machine has started!';
  } else {
    btn.value = 'Start machine';
    txt.textContent = 'The machine is stopped.';
  }
}

위 자바스크립트 코드는 DOM에서 <input> 을 나타내는HTMLInputElement 오브젝트를 참조하여 변수 btn 에 저장합니다. addEventListener() 은 버튼에click 이벤트가 발생할때 실행되는 함수를 생성합나디.

Adding keyboard shortcuts to buttons

Keyboard shortcuts, also known as access keys and keyboard equivalents, let the user trigger a button using a key or combination of keys on the keyboard. To add a keyboard shortcut to a button — just as you would with any <input> for which it makes sense — you use the accesskey global attribute.

In this example, s is specified as the access key (you'll need to press s plus the particular modifier keys for your browser/OS combination; see accesskey for a useful list of those).

<form>
  <input type="button" value="Start machine" accesskey="s">
</form>
<p>The machine is stopped.</p>

Note: The problem with the above example of course is that the user will not know what the access key is! In a real site, you'd have to provide this information in a way that doesn't intefere with the site design (for example by providing an easily accessible link that points to information on what the site accesskeys are).

Disabling and enabling a button

To disable a button, simply specify the disabled global attribute on it, like so:

<input type="button" value="Disable me" disabled>

You can enable and disable buttons at run time by simply setting disabled to true or false. In this example our button starts off enabled, but if you press it, it is disabled using btn.disabled = true. A setTimeout() function is then used to rest the button back to its enabled state after two seconds.

If the disabled attribute isn't specified, the button inherits its disabled state from its parent element. This makes it possible to enable and disable groups of elements all at once by enclosing them in a container such as a <fieldset> element, and then setting disabled on the container.

The example below shows this in action. This is very similar to the previous example, except that the disabled attribute is set on the <fieldset> when the first button is pressed — this causes all three buttons to be disabled until the two second timeout has passed.

Note: Firefox will, unlike other browsers, by default, persist the dynamic disabled state of a <button> across page loads. Use the autocomplete attribute to control this feature.

Validation

Buttons don't participate in constraint validation; they have no real value to be constrained.

Examples

The below example shows a very simple drawing app created using a <canvas> element and some simple CSS and JavaScript (we'll hide the CSS for brevity). The top two controls allow you to choose the color and size of the drawing pen. The button, when clicked, invokes a function that clears the canvas.

<div class="toolbar">
  <input type="color" aria-label="select pen color">
  <input type="range" min="2" max="50" value="30" aria-label="select pen size"><span class="output">30</span>
  <input type="button" value="Clear canvas">
</div>

<canvas class="myCanvas">
  <p>Add suitable fallback here.</p>
</canvas>
var canvas = document.querySelector('.myCanvas');
var width = canvas.width = window.innerWidth;
var height = canvas.height = window.innerHeight-85;
var ctx = canvas.getContext('2d');

ctx.fillStyle = 'rgb(0,0,0)';
ctx.fillRect(0,0,width,height);

var colorPicker = document.querySelector('input[type="color"]');
var sizePicker = document.querySelector('input[type="range"]');
var output = document.querySelector('.output');
var clearBtn = document.querySelector('input[type="button"]');

// covert degrees to radians
function degToRad(degrees) {
  return degrees * Math.PI / 180;
};

// update sizepicker output value

sizePicker.oninput = function() {
  output.textContent = sizePicker.value;
}

// store mouse pointer coordinates, and whether the button is pressed
var curX;
var curY;
var pressed = false;

// update mouse pointer coordinates
document.onmousemove = function(e) {
  curX = (window.Event) ? e.pageX : e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
  curY = (window.Event) ? e.pageY : e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}

canvas.onmousedown = function() {
  pressed = true;
};

canvas.onmouseup = function() {
  pressed = false;
}

clearBtn.onclick = function() {
  ctx.fillStyle = 'rgb(0,0,0)';
  ctx.fillRect(0,0,width,height);
}

function draw() {
  if(pressed) {
    ctx.fillStyle = colorPicker.value;
    ctx.beginPath();
    ctx.arc(curX, curY-85, sizePicker.value, degToRad(0), degToRad(360), false);
    ctx.fill();
  }

  requestAnimationFrame(draw);
}

draw();

Specifications

Specification Status
HTML Living Standard
The definition of '<input type="button">' in that specification.
Living Standard
HTML5
The definition of '<input type="button">' in that specification.
Recommendation

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 1Edge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support 1WebView Android Full support YesChrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

See also

문서 태그 및 공헌자

이 페이지의 공헌자: urty5656, JaeWorld
최종 변경자: urty5656,