admin管理员组文章数量:1429327
I know this is not standard but I want to set the value of a div, like this:
<div id="div" value="1">
document.getElementById('div').value = 2;
to use the value you have to use document.getElementById('div').getAttribute('value')
but I cant find any way to set it. thanks in advance!
I know this is not standard but I want to set the value of a div, like this:
<div id="div" value="1">
document.getElementById('div').value = 2;
to use the value you have to use document.getElementById('div').getAttribute('value')
but I cant find any way to set it. thanks in advance!
-
2
div
's don't have a value attribute. You can use a data- attribute to store a value which can then be retrieved. – Rob Moll Commented Oct 14, 2021 at 1:03
1 Answer
Reset to default 4You can't set a value to a div. Instead you can try to add a data attribute, like this:
<div id="div" data-value="1">
And if you want to access the data-value from javascript, just use
elem.dataset.{data_attribute_name};
, in this case: document.getElementById('div').dataset.value;
Note that you can set multiple data attributes to one element, making it much more versatile than using value.
本文标签: javascriptHow could I set the value of a divStack Overflow
版权声明:本文标题:javascript - How could I set the value of a div? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745473069a2659837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论