简体   繁体   中英

How to replace html tags using jquery

I want to change div and span tags to tr td using java script. But java script replace function only replace the first occurrence and leaves rest as it was.

<div style="width:100%">
  <div style="border-bottom:1px solid #DDDDDD; padding:10px 0px; height:100%; overflow:hidden;">
    <span style="width:420px; display:block; float:left; border-bottom:1px thin #333333;background:#F2F2F2">
      Custom Stirrups : Category - Adult ,Size-One size fits most 
    </span> 
    <span style="width:220px; display:block; float:left; border-bottom:1px thin #333333;background:#F2F2F2">
      NO
    </span>
    <span style="width:140px; display:block; float:left; border-bottom:1px thin #333333;background:#F2F2F2">
      1
    </span><br />
  </div>
  <div style="border-bottom:1px solid #DDDDDD; padding:10px 0px; height:100%; overflow:hidden;"> 
    <span style="width:420px; display:block; float:left; border-bottom:1px thin #333333;background:#F2F2F2">
      Flat Visor Fitted : Fitted Sizes - 7 1/8 
    </span> 
    <span style="width:220px; display:block; float:left; border-bottom:1px thin #333333;background:#F2F2F2">
      NO 
    </span> 
    <span style="width:140px; display:block; float:left; border-bottom:1px thin #333333;background:#F2F2F2">
      1
    </span>
    <br />
  </div>
</div>

I want something similar to this

<table width="100%">
        <tbody>
          <tr>
            <td width="471" style="background:#F2F2F2">Cap : Fitted Sizes - 6 5/8 </td>
            <td width="240" style="background:#F2F2F2">YES</td>
            <td width="145" style="background:#F2F2F2">1</td>
          </tr>
          <tr>
            <td width="471" style="background:#F2F2F2">Grey Dri-fit Undershirt : Size - XXL </td>
            <td width="240" style="background:#F2F2F2">YES</td>
            <td width="145" style="background:#F2F2F2">1</td>
          </tr>
         </tbody>
      </table>

this is my script which i was trying...please let me know where im wrong

var data = $('#dvData').html();
  data = data.replace('/<div style="width:100%">/g', '<table width="100%"><tbody><tr>');
  data = data.replace('/</div>/g', '</tbody></table></tr>');

Thanks in advance..

See http://www.w3schools.com/jsref/jsref_replace.asp It's for global replacement:

var str="Mr Blue has a blue house and a blue car";
var n=str.replace(/blue/g,"red");

Other examples are provided there as well.

I see you tagged also jquery, so assume you also accept jquery?

You could use .replaceWith() for this.

$('span, div, tr , td').replaceWith('Replaced!');

well , when dom is parsed it unchangable/immutable

you can try this:

$('div').contents().unwrap().wrap('<tr/>');

look at the tr tag there...

of if it dosent works try this

$(this).replaceWith($('<tr>' + this.innerHTML + '</tr>'));

you can try something like this..I know its not the best way to do it...

var data = $('#dvData').html();
  data = data.toString();
    data = data.replace('<div style="width:100%">', '<table width="100%">');
data = data.replace('span', 'td');

So try using .replaceAll(). if it doesn't help, use a loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM