简体   繁体   English

从NodeList提取属性值

[英]Extract Property value from NodeList

Inside a javascript function Im accessing a text box with a value of 'c29TMlzE4vmFlJHieICpso_u04oa'. 在javascript函数内部,我正在访问值为'c29TMlzE4vmFlJHieICpso_u04oa'的文本框。 Below is the javascript function i'm using. 以下是我正在使用的javascript函数。

 function test(){
        var txt = document.getElementsByName("consumerKey");
        alert(txt.item[0].getPropertyValue);
    }

The alert shows as 'undefined'. 警报显示为“未定义”。 In console I'm getting below as NodeList value of the txt. 在控制台中,我将获得以下txt的NodeList值。

NodeList[input#consumerKey property value ="c29TMlzE4vmFlJHieICpso_u04oa"  attribute value = "null"]

How can I extract 'c29TMlzE4vmFlJHieICpso_u04oa' from the nodelist. 如何从节点列表中提取“ c29TMlzE4vmFlJHieICpso_u04oa”。

Thanks 谢谢

item is a method, so you call it with a parameter: item是一种方法,因此您可以使用参数来调用它:

txt.item(0)

Or you could access it as 或者您可以通过以下方式访问它

txt[0]

getPropertyValue is for getting CSS property values from a style object, and is not relevant here. getPropertyValue用于从样式对象获取CSS属性值,此处不相关。 You simply want to access the value member of the input element: 您只想访问input元素的value成员:

txt.item(0).value
txt[0].value

However, there is really no need to use name attributes, except in special situations such as to group radio buttons. 但是,除了在特殊情况下(例如对单选按钮进行分组),实际上没有必要使用名称属性。 You're better off using IDs and getElementById . 您最好使用ID和getElementById Then you don't have to worry about taking the first item. 然后,您不必担心拿到第一件物品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM