我有一个 div
就像
<div class="firstclass" id="first" data-value="firstvalue">SomeThing</div>
在这里,我想通过 document.getElementById('first').value
或类似的方式获取 data-value
中的值..
如何获取这个值或者是否有类似的方法
最佳答案
使用.attr()
或.getAttribute()
。那行得通。
jQuery 解决方案
$(function(){
console.log($('#first').attr('data-value'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="firstclass" id="first" data-value="firstvalue">SomeThing</div>
JavaScript 解决方案
function Example(){
console.log(document.getElementById('first').getAttribute("data-value"));
}
Example();
<div class="firstclass" id="first" data-value="firstvalue">SomeThing</div>
关于javascript - HTML 将值存储到 div 中并获取其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39267019/