在jQuery中添加属性 [英] Adding attribute in jQuery

查看:91
本文介绍了在jQuery中添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在jQuery中将属性添加到特定的HTML标记中?



例如,像这样简单的HTML:

 < input id =someid/> 

然后添加如下属性disabled =true:

 < input id =someiddisabled =true/> 


解决方案

您可以使用 attr 就像这样:

  $('#someid')。attr('name','值'); 

然而,对于像选中的这样的DOM属性, disabled readonly ,正确的做法是(使用JQuery 1.6)使用 prop('disabled',true)> prop

  $('#someid' ; 


How can I add an attribute into specific HTML tags in jQuery?

For example, like this simple HTML:

<input id="someid" />

Then adding an attribute disabled="true" like this:

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

解决方案

You can add attributes using attr like so:

$('#someid').attr('name', 'value');

However, for DOM properties like checked, disabled and readonly, the proper way to do this (as of JQuery 1.6) is to use prop.

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

这篇关于在jQuery中添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆