如何使用jquery更改属性?

时间:2016-05-11 09:22:40

标签: javascript jquery

我有这个标签

SELECT DISTINCT CountryCode,CountryName FROM TheSingleTable

如何更改属性以使其

<div data-type="move" >some text</div>

2 个答案:

答案 0 :(得分:2)

删除现有属性并添加新

&#13;
&#13;
$('[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
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div data-type="move">some text</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

只需2行:

<div class="myDiv" data-type="move" >some text</div>

$('.myDiv').removeAttr('data-type'); // first remove old attribute
$('.myDiv').attr('data-ty',"move"); // now add new attribute