开发者

Change Element's node name?

开发者 https://www.devze.com 2022-12-31 07:06 出处:网络
Is it possible to change the element\'s node name in GWT? I mean something like this: HTML h = new HTML();

Is it possible to change the element's node name in GWT? I mean something like this:

HTML h = new HTML();
h.getElement().setNodeName("mydiv")

while there is no setNodeName() m开发者_运维技巧ethod for Element.

I'd like to acquire <mydiv>some contents</mydiv> instead of default tag <div>some contents</div>

Thanks for any hints.


You can't change the element node name of the HTML widget. However, you can create your own tag with Document.get().createElement("mydiv"), and use that to create a new Widget by extending Composite. However, I'm not sure why you want to do this, because adding new tags to the DOM and thereby extending HTML doesn't sound as something you should want. Setting the content in this tag isn't possible via methods like innerText because they are only available for valid tags.


change the tag name while keeping content and attributes

function changeTagName(elm,new_tag_name){
  var newElm = document.createElement(new_tag_name)
  var atr = elm.attributes;       
  for(var i=0;i<atr.length;i++){  // copy all atributtes
     newElm.setAttribute(atr[i].name,atr[i].value)
  } 
  document.body.insertBefore(newElm,elm)
  newElm.innerHTML=elm.innerHTML; //copy the content
  elm.parentNode.removeChild(elm)  // remove original 
}

for example:

<span id='sp1' class='cl1 cl2'> some t  e x t with   (\n)         gaps .... and etc</span>

changeTagName(document.getElementById('sp1'),'pre');
0

精彩评论

暂无评论...
验证码 换一张
取 消