[英]How To Get Value Of Span Class Title Value
I would like JS to return the text value of title which is 'lost' from this line of code:我希望 JS 返回从这行代码中“丢失”的标题的文本值:
I want to print to console only the text "lost".我只想打印到控制台“丢失”的文本。
I tried the following (which didn't work):我尝试了以下(不起作用):
console.log(document.getElementsByClassName('_3rAzIK1XDa7ajuyjz8OJkl _12pssoQOCYKCt5amUeIK')[0].getElementsByClassName('title')[0].innerText)
<span class="_3rAzIK1XDa7ajuyjz8OJkl _12pssoQOCYKCt5amUeIK" data-qa="text-recent-bet-status" title="lost"><i class="betgames-icon closed"></i></span>
title
isn't a class name, it's an attribute. title
不是类名,它是一个属性。 Use .getAttribute()
to get its value.使用.getAttribute()
获取其值。
console.log(document.getElementsByClassName('_3rAzIK1XDa7ajuyjz8OJkl _12pssoQOCYKCt5amUeIK')[0].getAttribute('title'));
<span class="_3rAzIK1XDa7ajuyjz8OJkl _12pssoQOCYKCt5amUeIK" data-qa="text-recent-bet-status" title="lost"><i class="betgames-icon closed"></i></span>
Use getAttribute() to get the value of the title
attribute of selected DOM element:.使用getAttribute()获取所选 DOM 元素的title
属性值:。 Here title
is an Attribute and not a class.这里的title
是一个属性而不是一个类。
console.log(document.getElementsByClassName('_3rAzIK1XDa7ajuyjz8OJkl _12pssoQOCYKCt5amUeIK')[0].getAttribute('title'))
<span class="_3rAzIK1XDa7ajuyjz8OJkl _12pssoQOCYKCt5amUeIK" data-qa="text-recent-bet-status" title="lost"><i class="betgames-icon closed"></i></span>
document.getElementsByClassName
returns the elements array based on that className
. document.getElementsByClassName
返回基于该className
的元素数组。
And to get the title
attribute of the selected element, you can use getAttribute
function.要获取所选元素的title
属性,可以使用getAttribute
函数。
console.log(document.getElementsByClassName('_3rAzIK1XDa7ajuyjz8OJkl')[0].getAttribute('title'));
<span class="_3rAzIK1XDa7ajuyjz8OJkl _12pssoQOCYKCt5amUeIK" data-qa="text-recent-bet-status" title="lost"><i class="betgames-icon closed"></i></span>
console.log(document.getElementsByClassName('_3rAzIK1XDa7ajuyjz8OJkl _12pssoQOCYKCt5amUeIK')[0].title)
您可以直接访问标题等默认属性或使用 getAttribute('title') 代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.