如何在代码中获取attr属性的值

如何在代码中获取attr属性的值,第1张

在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() 方法回收资源。

先给你看下面两段代码:

<div id="div1">test!</div>

<script>

   consolelog(documentgetElementById("div1"));  //这个能够获得值

</script><script>

   consolelog(documentgetElementById("div1"));  //这个不能获得值!

</script>

<div id="div1">test!</div>

看出原因没有?当js代码是先于html运行时,是获取不到DOM元素的!因为这时候该DOM元素尚未加载到内存,相当于不存在!

要想实现js代码放到任何位置都能获得DOM元素,就要保证代码在页面完全加载完毕后才开始运行,比如:

<script>

   windowonload=function(){  //网页完全加载完毕才会触发这个事件

      consolelog(documentgetElementById("div1")); //这时候就能获得了

   }

</script>

<div id="div1">test!</div>

1JS方法:

<script>windowonload=function(){

var va = documentgetElementById("d1")childNodes;

for(var i=0;i<valength;i++){

if(va[i]nodeNametoLocaleUpperCase() == "A"){

alert("JS: "+va[i]attributes["linkId"]nodeValue);

}

}

};

</script>

2 Jquery方法:

<script src="jquery-191minjs" type="text/javascript"></script>

<script>

$(document)ready(function(){

$("#d1 a")each(function(){

alert("Jquery: "+$(this)attr("linkId"));

});

});

</script>

JS 获取HTML标签内的子节点的方法

子节点的个数:

documentgetElementById("id")childNodeslength  

注意: 标签开/闭合算2个节点 第几个子几点:

documentgetElementById("id")childNodes[n]  

示例:

这里是 length-4 处,margin-left:20px

输出:length=8

实例:

<div id="page_kx" style="text-align: center;" class="tac">    <span class="fy2">1</span>  <a href="#">2</a>  <a href="#">3</a>  <a href="#">4</a>  <a href="#">5</a>  <a href="#">下一页</a>  <a href="#">末页</a>  </div>    <script>  var gor=documentgetElementById("page_kx");  var gorL=Number(gorchildNodeslength)-4;  gorchildNodes[gorL]stylemargin="0 0 0 20px";  </script>  

div是没有value属性的,但是我们可以给它加上去也是可以获得的,在这里value只是一个自定义参数。

html代码

<div class="store"  value="tanyu"></div>

jq代码

$(function(){

$("store")click(function(){

alert($(this)attr('value'));

});

});

json中取出对象的属性值步骤如下:

1、打开vscode,创建一个测试页面JsonTesthtml,用于演示在js中如何获取json对象的属性集合。

2、在测试页面中,定义一个js变量,将其赋值为json格式的字符串,用于模拟从后台返回过来的json字符串值,以及后续转换为json对象,和获取其属性集合。var json = "[{\"Col1\": \"Hello\", \"Col2\": \"World\"}, {\"Col1\": \"您好\", \"Col2\": \"世界\"}]"。

3、使用js的eval函数,将json字符串的变量值,转换为json对象。var $json = eval("(" + json + ")");

4、得到json对象之后,使用for-in循环语句,得到json对象的序号,也就是,这个json对象中,还有多少个子对象。

5、因为js是弱类型的语言,并不要求json对象中的每个子对象属性都一样。所以,在得到序号之后,使用Objectkeys($json[i]); 就能获取到当前json子对象的属性集合。

6、如果想要获取到json对象的每个具体的列名和值,就需要再加一个for循环,遍历列名,得到列名和值。

C c = new C(); Type t = typeof(C); var v1 = tGetField("n")GetValue(c);//获取字段值,你的类Cn是字段,不是属性 var v2 = tGetProperty("n")GetValue(c, null);//获取属性值 //两个参数的GetValue向下兼容net framework 20 //////////////////////////////////////////// if(typeof(C)GetField("n")GetValue(c)Equals(1))

第一种通过获取

第二种通过[]获取

那什么时候用点获取什么时候[]呢,可以在不同场景使用

两个的相同点:都是获取对象属性值

不同点:

1 [ ]运算符可以使用字符串变量的内容作为属性名,点运算符不能

普通常量赋值的时候可以用点运算符,其他可以用[]运算符,

以上就是关于如何在代码中获取attr属性的值全部的内容,包括:如何在代码中获取attr属性的值、javascript 如何获得html DOM节点的属性值、html JS中如何获取某ID下的某个标签的某个属性值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存