jquery怎么获得元素的属性id值

jquery怎么获得元素的属性id值,第1张

$("#textId")attr("width")是获取Id为textId的控件的width属性

$("className")attr("width")是获取包含class名为className的控件的width属性。

$("[name=textName]")attr("width")是获tml xmlns=">

取name为textName的控件的width属性。

但要注意,jquery取到的可能是一个数组,如果确定属性为同一值,则没问题,否则应该循环取得控件,再取属性值。

如:$("#textId")[0]width是取第一个控件的width属性。

但也有写属性用attr取不到,也需要用角标的方式取得控件后在取属性。

有时候我们需要把颜色,数值写成attr属性,这样做是为了屏蔽开发者对应具体数值,比如我们需要设置不同主题下的主色,副色,或者是不同版本的ActionBar大小,亦或者是不同Dpi下的DrawerLayout的宽度等。

在xml里,我们可以简单的引用attr属性值,例如:

android:background="attr/colorPrimary"

android:minHeight="attr/actionBarSize"

当然,我们有时候也需要在代码中获取attr属性值:

TypedValue typedValue = new TypedValue();

contextgetTheme()resolveAttribute(RattryourAttr, typedValue, true);

// For string

typedValuestring

typedValuecoerceToString()

// For other data

typedValueresourceId

typedValuedata;

获取arrt样式中的值

以上是针对个体数值根据不同类型来获取的,如果想要获取 style 的话,需要在拿到 resourceId 之后再进一步获取具体数值,以 TextAppearanceLarge 为例:

<style name="TextAppearanceLarge">

<item name="android:textSize">22sp</item>

<item name="android:textStyle">normal</item>

<item name="android:textColor">textColorPrimary</item>

</style>

TypedValue typedValue = new TypedValue();

contextgetTheme()resolveAttribute(androidRattrtextAppearanceLarge, typedValue, true);

int[] attribute = new int[] { androidRattrtextSize };

TypedArray array = contextobtainStyledAttributes(typedValueresourceId, attribute);

int textSize = arraygetDimensionPixelSize(0 / index /, -1 / default size /);

arrayrecycle();

注意,要记得调用 TypedArrayrecycle() 方法回收资源。

name是input标签的属性值,jQuery提供了attr() 方法用于设置/改变属性值

$("input:text")attr("name");

$("input:text")prop("name");  // 也可以使用prop()方法获取属性

$("[name='name']")val(); //获取vlaue值

$("[name='name']")attr('id','name'); //添加id名称

$("[name='name']")after('

jquery通过name属性取值的方法

 alert($("input[name='inputTest']")val());

alert($("input[type='text']")attr("id")); //这个是取id的值,方便记就写在这儿了

$("input[name='inputTest']")each(

function(){

alert($(this)val());

}

alert($("input[name='inputTest']")[0]value);

alert($("input[name='inputTest']")[1]value);

alert($("input[name='inputTest']")get(0)value); //此处也可以得到值,等同于上面两行

alert($("input[name='inputTest']")get(1)value);

根据name取值:  

$("input[name='mobile']")val()  

根据id取值:  

$("#mobile_reg_form")html()  

根据name取值了遍历:  

$("input[name='mobile']")each(  

function(){  

alert($(this)val());  

}  

)   

取出form中的input:  

$(document)ready(function(){    

vara=$("form input");    

$each(    

    a,    

function(name,object){    

alert(name+":"+$(object)val());    

}    

);    

});    

得到值(多个的情况):  

$("input[name='mobile']")[0]value  

$("input[name='mobile']")get(1)value  

-----jquery添加删除样式--------  

给一个标签添加样式:  

$("#id")addClass("style");  

删除一个标签的样式:  

$("#id")removeClass("style");  

注:"#id"  id是对应标签的id,style是对应css样式的名称  

覆盖模型层对象的toString方法,进行重写,写有意义的内容,把属性全都写进去,

然后直接对象toString()把所有的打印出来

给你文档最后一条参考

以上就是关于jquery怎么获得元素的属性id值全部的内容,包括:jquery怎么获得元素的属性id值、如何在代码中获取attr属性的值、jquery通过name,id名称获取当前value值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10146215.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存