199 views

Замена тегов JS

Если нужно заменить тег например div на form можно использовать конструкцию

/**
 * REPLACE TAG
 * $('#replaceForm').replaceTag('<span id="replaceTag">',true);
* */
$.extend({
 replaceTag: function (currentElem, newTagObj, keepProps) {
  var $currentElem = $(currentElem);
  var i, $newTag = $(newTagObj).clone();
  if (keepProps) {//{{{
   newTag = $newTag[0];
   newTag.className = currentElem.className;
   $.extend(newTag.classList, currentElem.classList);
   $.extend(newTag.attributes, currentElem.attributes);
  }//}}}
  $currentElem.wrapAll($newTag);
  $currentElem.contents().unwrap();
  // return node; (Error spotted by Frank van Luijn)
  return this; // Suggested by ColeLawrence
 }
});

$.fn.extend({
 replaceTag: function (newTagObj, keepProps) {
  // "return" suggested by ColeLawrence
  return this.each(function() {
   jQuery.replaceTag(this, newTagObj, keepProps);
  });
 }
});
This site is using the Seo Wizard wordpress plugin by www.seowizard.org.