我有这个标签
<div data-type="move" >some text</div>
如何更改属性,使其成为
<div data-ty="move" >some text</div>
最佳答案
删除现有属性并添加新属性
$('[data-type]').each(function() { // get all elemnts which have the attribute `data-type` and iterate over them
var temp = $(this).data('type'); // get the existing element attribute value
$(this)
.removeAttr('data-type') // remove the attribute
.attr('data-ty', temp); // add new attribute with value of existing
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div data-type="move">some text</div>
关于javascript - 如何使用jquery更改属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37158256/