android 怎么在代码中获取控件的属性值

android 怎么在代码中获取控件的属性值,第1张

如果是自定义的控件可以用一下代码TypedArray ta = contextobtainStyledAttributes(attrs, RstyleableMyToggleBtn);// 由attrs 获得 TypeArray,

如果是系统自带的控件,通常控件点get会有对应的获取属性值的方法,如textViewgetHeight,不过有些方法如margin,就要通过layoutparam去获取设置

let obj=documentgetElementById("XXXXXX");

jquery中用法:

设置属性:objattr("属性名",'属性值')

获取属性值:属性值=objattr('属性名')

原生用法:

设置属性:objsetAttribute("属性名",'属性值')

获取属性值:属性值=objgetAttribute('属性名')

使用JQuery给自定义属性赋值取值

jQuery 属性 *** 作 - attr() 方法

定义和用法

attr() 方法设置或返回被选元素的属性值。

一、返回属性值

返回被选元素的属性值。

语法

$(selector)attr(attribute)

参数描述

attribute    规定要获取其值的属性。

$(selector)attr(attribute)

<html>

<head>

<script type="text/javascript" src="/jquery/jqueryjs"></script>

<script type="text/javascript">

$(document)ready(function(){

  $("button")click(function(){

    alert("Image width " + $("img")attr("width"));

  });

});

</script>

</head>

<body>

<img src="/i/eg_smilegif" width="128" height="128" />

<br />

<button>返回图像的宽度</button>

</body>

</html>

二、设置属性/值

设置被选元素的属性和值。

语法

$(selector)attr(attribute,value)

参数描述

attribute    规定属性的名称。    

value    规定属性的值。

$(selector)attr(attribute,value)

<html>

<head>

<script type="text/javascript" src="/jquery/jqueryjs"></script>

<script type="text/javascript">

$(document)ready(function(){

  $("button")click(function(){

    $("img")attr("width","180");

  });

});

</script>

</head>

<body>

<img src="/i/eg_smilegif" />

<br />

<button>设置图像的 width 属性</button>

</body>

</html>

以上就是关于android 怎么在代码中获取控件的属性值全部的内容,包括:android 怎么在代码中获取控件的属性值、为什么我的js代码中attr识别不了、如何使用JQuery给自定义属性赋值取值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存