0

についてお聞きしたいだけですjQuery。これを行うことができjQueryます:

<h1 id="lorem-ipsum">Hello World!</h1>

これに:

<div id="lorem-ipsum"></div>
<h1>Hello World!</h1>
4

3 に答える 3

3

You mean like this man: working demo Bit dynamic version: http://jsfiddle.net/QSdqM/ or http://jsfiddle.net/GQzFU/

Hope it helps the cause :)

API: http://api.jquery.com/replaceWith/

code

$('h1').replaceWith(function() {

   return '<div id="' + this.id + '"></div><h1>' + $(this).text() + '</h1>'

});

OR

$('h1').replaceWith('<div id="lorem-ipsum"></div><h1>Hello World!</h1>');


alert($('body').html());​
于 2012-07-23T04:30:49.070 に答える
1
var myDiv = $('<div></div>').attr('id', 'lorem-ipsum');
$('h1#lorem-ipsum').removeAttr('id').before(myDiv);
于 2012-07-23T04:31:03.830 に答える
1

次のことを試してください。

var txt = $('#lorem-ipsum').text()
$('#lorem-ipsum')
    .after($('<h1/>').text(txt))
    .replaceWith('<div id="#lorem-ipsum"/>')

デモ

于 2012-07-23T04:37:28.343 に答える