我有包含元素的 html 页面:
<div id="location-id" class="form-control">
<span class="hub-trigger" data-hub-recordid="28">Germany</span>
</div>
我使用var element= document.getElementById('location-id')
获取html元素。
请告诉我如何获得28
来自span
?
我尝试使用 var id = element.getElementByTagName('data-hub-recordid')
与 val()
, text()
, innerText()
最后但它没有像我预期的那样工作。
最佳答案
尝试一下
document.querySelector('.hub-trigger').getAttribute('data-hub-recordid')
或者它
const parent = document.querySelector('#location-id');
const child = parent.querySelector('.hub-trigger');
const recordid = child.getAttribute('data-hub-recordid');
关于javascript - 如何使用javascript从子元素中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57290230/