JavaScript 修改 div 元素 data-options 属性值
To modify the value of the 'data-options' attribute in a div using JavaScript, you can follow these steps:
- Get a reference to the div element using
document.getElementByIdor any other method that suits your needs. For example, if the div has an id attribute of 'myDiv', you can get a reference to it like this:
var divElement = document.getElementById('myDiv');
- Access the 'data-options' attribute using the
getAttributemethod and store its value in a variable. For example:
var options = divElement.getAttribute('data-options');
- Modify the value of the
optionsvariable as per your requirements. For example, you can update it to a new value:
options = 'new value';
- Set the updated value of the 'data-options' attribute using the
setAttributemethod. For example:
divElement.setAttribute('data-options', options);
Here's a complete example:
<div id='myDiv' data-options='initial value'>...</div>
<script>
var divElement = document.getElementById('myDiv');
var options = divElement.getAttribute('data-options');
options = 'new value';
divElement.setAttribute('data-options', options);
</script>
After executing this JavaScript code, the 'data-options' attribute in the div element will be updated to the new value.
原文地址: https://www.cveoy.top/t/topic/pQKA 著作权归作者所有。请勿转载和采集!