jQuery에서 특정 HTML 태그에 속성을 추가하려면 어떻게해야합니까?
예를 들어,이 간단한 HTML과 같이 :
<input id="someid" />
그런 다음 다음과 같이 disabled = “true”속성을 추가하십시오.
<input id="someid" disabled="true" />
답변
다음 attr
과 같이 사용하여 속성을 추가 할 수 있습니다 .
$('#someid').attr('name', 'value');
DOM 속성이 원하는에 대한 그러나 checked
, disabled
그리고 readonly
, (JQuery와 1.6 기준)이 작업을 수행 할 수있는 적절한 방법을 사용하는 것입니다 prop
.
$('#someid').prop('disabled', true);
답변
최상의 솔루션 : jQuery v1.6에서 prop () 을 사용 하여 속성을 추가 할 수 있습니다
$('#someid').prop('disabled', true);
그것을 제거하려면 removeProp()
$('#someid').removeProp('disabled');
또한 이러한 속성을 false로 설정하는 데 .removeProp () 메서드를 사용해서는 안됩니다. 기본 속성이 제거되면 다시 추가 할 수 없습니다. 자세한 내용은 .removeProp ()를 참조하십시오.
답변
.attr
속성을 설정하는 jQuery의 기능 으로이 작업을 수행 할 수 있습니다 . 그것들을 제거하는 것은 .removeAttr
기능을 통해 수행됩니다 .
//.attr()
$("element").attr("id", "newId");
$("element").attr("disabled", true);
//.removeAttr()
$("element").removeAttr("id");
$("element").removeAttr("disabled");
답변
$('#someid').attr('disabled', 'true');
답변
$('#someid').attr('disabled', 'true');
답변
$('.some_selector').attr('disabled', true);
답변
다음과 같이 속성을 추가하십시오.
$('#Selector_id').attr('disabled',true);