Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Изменить тэг html (https://javascript.ru/forum/jquery/62184-izmenit-tehg-html.html)

skillful 28.03.2016 16:23

Изменить тэг html
 
Здравствуйте. Допустим существует такой html код
<button id="redirect" onclick="" type="submit" class="">
Текст
</button>

Как с помощью jquery сделать, чтобы получилось так:
<a id="redirect" onclick="" type="submit" class="">
Отмена
</a>

Dinar 28.03.2016 19:29

replace_tag($('button'), 'a');

function replace_tag(node, replacementTag){

	node.each(function() {
		var outer = this.outerHTML;

		var regex = new RegExp('<' + this.tagName, 'i');
		var newTag = outer.replace(regex, '<' + replacementTag);

		regex = new RegExp('</' + this.tagName, 'i');
		newTag = newTag.replace(regex, '</' + replacementTag);

		$(this).replaceWith(newTag);
	});
}

krasovsky 29.03.2016 13:24

http://api.jquery.com/replacewith/

skillful 29.03.2016 20:29

Dinar,
krasovsky,
Спасибо, получилось


Часовой пояс GMT +3, время: 15:21.