javascript - 如何使用jquery更改属性?

标签 javascript jquery

我有这个标签

<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/

相关文章:

javascript - 如何使用node.js在server.js中传递参数

javascript - 检查元素类名

jquery - 在等高列中定位元素

jquery - 使用 jquery 和动态生成的标签 ID 获取文本框值?

JavaScript 将值拆分为数组

javascript - 如何使用 jQuery 管理嵌套类?

javascript - 从 iframe 内部获取鼠标绝对位置

javascript - illustrator 自动映射到 imagemap?或者,还有更好的方法?

javascript - React, Redux - 使用 Redux 修改父组件

javascript - 如何将 rafael.js 和 paper.js 合并在一个 html 中?