0

Say I have a div as:

<div id="myDiv"> </div>

I have a GQuery object which I got using

GQuery mydiv = GQuery.$("#myDiv");

Using this, I want to create a new parent which wraps this div element inside it. For example, if the parent is another div, following is what I want:

<div id = "parentDiv"> 
   <div id="myDiv"> </div>
</div>

Though this sounds like a simple thing to do, I am not able to get the desired result.

Note: I have tagged JQuery for this question as well since if a simple method for the same exists in JQuery, it is probable that it would exist in GQuery as well.

4

2 回答 2

5

我想你想使用wrap()

GQuery mydiv = GQuery.$("#myDiv").wrap('<div id="parentDiv" />');

我不知道 GQuery,它基于 jQuery。


我阅读了一些文档,但不确定您的语法是否应该是:

GQuery mydiv = $("#myDiv").wrap('<div id="parentDiv" />');

或者是其他东西。只要确保你有良好的语法并使用wrap().

于 2012-06-05T13:01:58.367 回答
0

在 jQuery 中是这样的

$(document).ready(function(){
var parent = $("#myDiv").parent();
$("<div id='parentDiv'></div>").appendTo(parent)
                  $("#myDiv").appendTo($("#parentDiv"));
});​

http://jsfiddle.net/fZ6zx/

于 2012-06-05T13:07:23.623 回答