admin管理员组文章数量:1384587
I'm trying to get the tagname of name from the below line of code. I have to get the name from the below tagname using javascript
<preference name="webviewbounce" value="false" />
i need to get webviewbounce
This is what i know.
document.getElementsByTagName("preference")
But it doesnt give me the preference name. What i want is the tagname of name which is webviewbounce
I'm trying to get the tagname of name from the below line of code. I have to get the name from the below tagname using javascript
<preference name="webviewbounce" value="false" />
i need to get webviewbounce
This is what i know.
document.getElementsByTagName("preference")
But it doesnt give me the preference name. What i want is the tagname of name which is webviewbounce
6 Answers
Reset to default 2Use document.querySelector
to get the element. It will return the first matched element.Then use getAttribute
to get the required attribute from the element.
If there are multiple tag element with same tagname , use document.querySlectorAll
var getElem = document.querySelector('preference'),
getNameProperty = getElem.getAttribute('name');
console.log(getNameProperty)
<preference name="webviewbounce" value="false" />
Try:
document.getElementsByName("webviewbounce");
This will get the element that has the name of webviewbounce
getElementsByTagName
is going to return a collection of elements. You can then use getAttribute()
to get the name property of the first item in the collection.
console.log( document.getElementsByTagName( "preference" )[0].getAttribute( 'name' ) );
const p = document.getElementsByTagName('preference')
console.log(p[0])
// <preference name="webviewbounce" value="false">…</preference>
console.log(p[0].getAttribute('name'))
// webviewbounce
<preference name="webviewbounce" value="false" />
Considering this as your first element of preference tag. this would give document.getElementsByTagName("preference")["0"].name the name. The "0" in the line code should be changed to the exact element.
In addition you can also use getAttribute('name') with getElementsByTagName().
You can use getAttribute to get the name value of the tag.
You can try something like this.
var element = document.getElementByTagName("preference");
var name = element.getAttribute("name");
console.log(name);
本文标签: jqueryGet tagname name value javascriptStack Overflow
版权声明:本文标题:jquery - Get tagname name value javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744537054a2611379.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论