I would like to change all the names of the attributes where class="testingCase"
throughout all my whole html document.
我想在整个html文档中更改class =“testingCase”的所有属性名称。
e.g. Change:
例如更改:
<a class="testingCase" href="#" title="name of testing case">Blabla</a>
<a class="testingCase" href="#" title="name of another testing case">Bloo</a>
To this:
对此:
<a class="testingCase" href="#" newTitleName="name of testing case">Blabla</a>`
<a class="testingCase" href="#" newTitleName="name of another testing case">Bloo</a>`
I was thinking of a find and replace but that seems a lot of code for something so easy. Is there a jQuery function for this or a simple method?
我正在考虑一个查找和替换,但似乎很多代码很容易。这个或简单方法有jQuery函数吗?
7 个解决方案
#1
32
I don't think you can "rename" an attribute, but you can create new attributes and remove other ones...
我不认为你可以“重命名”一个属性,但是你可以创建新属性并删除其他属性......
$('a.testingCase[title]').each(function() {
var $t = $(this);
$t.attr({
newTitleName: $t.attr('title'),
})
.removeAttr('title');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="testingCase" href="#" title="name of testing case">Blabla</a>
<a class="testingCase" href="#" title="name of another testing case">Bloo</a>
Edit: added in the bit that makes it only select a
elements with class="testingCase"
编辑:在该位中添加,使其仅选择具有class =“testingCase”的元素
#2
8
I don't think you can change an attribute name but what you can do is :
我认为您不能更改属性名称,但您可以做的是:
- get all the
<a>
tags with a title attribute; - 获取所有带有title属性的标签;
- foreach of them, create a newTitleName attribute with the same value as the title;
- foreach,创建一个newTitleName属性,其值与标题相同;
- then delete the title attribute.
- 然后删除title属性。
JQuery let you do that with builtin functions this way :
JQuery允许您通过这种方式使用内置函数:
/* get all <a> with a "title" attribute that are testingCase
then apply an anonymous function on each of them */
$('a.testingCase[title]').each(function() {
/* create a jquery object from the <a> DOM object */
var $a_with_title = $(this);
/* add the new attribute with the title value */
$a_with_title.attr("newTitleName", $a_with_title.getAttribute('title'));
/* remove the old attribute */
$a_with_title.removeAttr('title');
});
#3
5
A little later but this link has a great jquery function extension:
稍后,但这个链接有一个很棒的jquery函数扩展:
jQuery.fn.extend({
renameAttr: function( name, newName, removeData ) {
var val;
return this.each(function() {
val = jQuery.attr( this, name );
jQuery.attr( this, newName, val );
jQuery.removeAttr( this, name );
// remove original data
if (removeData !== false){
jQuery.removeData( this, name.replace('data-','') );
}
});
}
});
Example
例
// $(selector).renameAttr(original-attr, new-attr, removeData);
// removeData flag is true by default
$('#test').renameAttr('data-test', 'data-new' );
// removeData flag set to false will not remove the
// .data("test") value
$('#test').renameAttr('data-test', 'data-new', false );
I DID NOT WRITE THIS. BUT I DID TEST IS WITH JQ 1.10. THE CODE IS FROM THE LINK.
我不写这个。但我测试的是JQ 1.10。该代码来自链接。
#4
2
Here's a simple jquery-style plugin that gives you a renameAttr method:
这是一个简单的jquery风格的插件,它为您提供了一个renameAttr方法:
// Rename an entity attribute
jQuery.fn.renameAttr = function(oldName, newName) {
var args = arguments[0] || {};
var o = $(this[0])
o
.attr(
newName, o.attr(oldName)
)
.removeAttr(oldName)
;
};
There is also this example which adds an option to remove the data for the old attribute.
还有一个示例添加了一个选项来删除旧属性的数据。
#5
1
One liner
一个班轮
$('selector').replaceWith($('selector')[0].outerHTML.replace("oldName=","newName="));
worked great for me when I needed to strip a prefix from all of my attributes
当我需要从所有属性中删除前缀时,对我来说很有用
$('selector').replaceWith($('selector')[0].outerHTML.(/prefix\-/g,""));
#6
-1
same answer as @nickf but cleaner code:
与@nickf相同的答案,但更清晰的代码:
$('a.testingCase[title]').each(function() {
var curElem = $(this);
curElem.attr("newTitleName", curElem.attr('title')).removeAttr('title');
});
#7
-1
It works for me!
这个对我有用!
$('input[name="descricao"]').attr('name', 'title');
#1
32
I don't think you can "rename" an attribute, but you can create new attributes and remove other ones...
我不认为你可以“重命名”一个属性,但是你可以创建新属性并删除其他属性......
$('a.testingCase[title]').each(function() {
var $t = $(this);
$t.attr({
newTitleName: $t.attr('title'),
})
.removeAttr('title');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="testingCase" href="#" title="name of testing case">Blabla</a>
<a class="testingCase" href="#" title="name of another testing case">Bloo</a>
Edit: added in the bit that makes it only select a
elements with class="testingCase"
编辑:在该位中添加,使其仅选择具有class =“testingCase”的元素
#2
8
I don't think you can change an attribute name but what you can do is :
我认为您不能更改属性名称,但您可以做的是:
- get all the
<a>
tags with a title attribute; - 获取所有带有title属性的标签;
- foreach of them, create a newTitleName attribute with the same value as the title;
- foreach,创建一个newTitleName属性,其值与标题相同;
- then delete the title attribute.
- 然后删除title属性。
JQuery let you do that with builtin functions this way :
JQuery允许您通过这种方式使用内置函数:
/* get all <a> with a "title" attribute that are testingCase
then apply an anonymous function on each of them */
$('a.testingCase[title]').each(function() {
/* create a jquery object from the <a> DOM object */
var $a_with_title = $(this);
/* add the new attribute with the title value */
$a_with_title.attr("newTitleName", $a_with_title.getAttribute('title'));
/* remove the old attribute */
$a_with_title.removeAttr('title');
});
#3
5
A little later but this link has a great jquery function extension:
稍后,但这个链接有一个很棒的jquery函数扩展:
jQuery.fn.extend({
renameAttr: function( name, newName, removeData ) {
var val;
return this.each(function() {
val = jQuery.attr( this, name );
jQuery.attr( this, newName, val );
jQuery.removeAttr( this, name );
// remove original data
if (removeData !== false){
jQuery.removeData( this, name.replace('data-','') );
}
});
}
});
Example
例
// $(selector).renameAttr(original-attr, new-attr, removeData);
// removeData flag is true by default
$('#test').renameAttr('data-test', 'data-new' );
// removeData flag set to false will not remove the
// .data("test") value
$('#test').renameAttr('data-test', 'data-new', false );
I DID NOT WRITE THIS. BUT I DID TEST IS WITH JQ 1.10. THE CODE IS FROM THE LINK.
我不写这个。但我测试的是JQ 1.10。该代码来自链接。
#4
2
Here's a simple jquery-style plugin that gives you a renameAttr method:
这是一个简单的jquery风格的插件,它为您提供了一个renameAttr方法:
// Rename an entity attribute
jQuery.fn.renameAttr = function(oldName, newName) {
var args = arguments[0] || {};
var o = $(this[0])
o
.attr(
newName, o.attr(oldName)
)
.removeAttr(oldName)
;
};
There is also this example which adds an option to remove the data for the old attribute.
还有一个示例添加了一个选项来删除旧属性的数据。
#5
1
One liner
一个班轮
$('selector').replaceWith($('selector')[0].outerHTML.replace("oldName=","newName="));
worked great for me when I needed to strip a prefix from all of my attributes
当我需要从所有属性中删除前缀时,对我来说很有用
$('selector').replaceWith($('selector')[0].outerHTML.(/prefix\-/g,""));
#6
-1
same answer as @nickf but cleaner code:
与@nickf相同的答案,但更清晰的代码:
$('a.testingCase[title]').each(function() {
var curElem = $(this);
curElem.attr("newTitleName", curElem.attr('title')).removeAttr('title');
});
#7
-1
It works for me!
这个对我有用!
$('input[name="descricao"]').attr('name', 'title');