3 回答

TA貢獻1784條經(jīng)驗 獲得超2個贊
您可以attr像這樣添加屬性:
$('#someid').attr('name', 'value');
但是,對于和這樣的DOM屬性checked,執(zhí)行此操作的正確方法(自JQuery 1.6起)是使用。disabledreadonlyprop
$('#someid').prop('disabled', true);

TA貢獻1799條經(jīng)驗 獲得超8個贊
最佳解決方案:從jQuery v1.6開始,您可以使用 prop()添加屬性
$('#someid').prop('disabled', true);
刪除它,使用 removeProp()
$('#someid').removeProp('disabled');
Reference
另請注意,不應(yīng)使用.removeProp()方法將這些屬性設(shè)置為false。一旦刪除本機屬性,就無法再次添加。有關(guān)更多信息,請參見.removeProp()。

TA貢獻1911條經(jīng)驗 獲得超7個贊
您可以使用jQuery的.attr函數(shù)來執(zhí)行此操作,該函數(shù)將設(shè)置屬性。通過.removeAttr功能刪除它們。
//.attr()
$("element").attr("id", "newId");
$("element").attr("disabled", true);
//.removeAttr()
$("element").removeAttr("id");
$("element").removeAttr("disabled");
添加回答
舉報