我如何添加一个属性到特定的HTML标签在jQuery?

例如,像这个简单的HTML:

<input id="someid" />

然后添加一个属性disabled="true",如下所示:

<input id="someid" disabled="true" />

当前回答

$('#yourid').prop('disabled', true);

其他回答

$('#someid').attr('disabled', 'true');
$('.some_selector').attr('disabled', true);

这可能会更有帮助....

$("element").prop("id", "modifiedId");
//for boolean
$("element").prop("disabled", true);
//also you can remove attribute
$('#someid').removeProp('disabled');

你可以使用jQuery的.attr函数来设置属性。删除它们是通过. removeattr函数完成的。

//.attr()
$("element").attr("id", "newId");
$("element").attr("disabled", true);

//.removeAttr()
$("element").removeAttr("id");
$("element").removeAttr("disabled");
$('#yourid').prop('disabled', true);