最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery modify div created from html before appending - Stack Overflow

programmeradmin0浏览0评论

I have a function that appends a div to another div.

Something like:

var div = ...some html

$("#mydiv").append(div);

However I want to do some stuff to the div I just added. Currently I want to hide a sub part of the new div, and add a object as data. Something like

 var div = ...some html   
 $("#mydiv").append(div);
 $(my new div).find(".subDiv").hide(); 
 $(my new div).data('user',object);

But how should I get the the new div I created? Is there a way to create it and then preform these actions, and then append it? Or should I append it and then retrieve it and modify it?

Efficiency is important as this will be iterated for search results...

Thanks!

I used this as my solution thanks to Tricker:

var div = ...a lagre piece of html;
var newDiv = $(div);
newDiv.find("[show='contractor']").hide();
newDiv.data('user', userObject);
$(appendDiv).append(newDiv);

Thanks!

I have a function that appends a div to another div.

Something like:

var div = ...some html

$("#mydiv").append(div);

However I want to do some stuff to the div I just added. Currently I want to hide a sub part of the new div, and add a object as data. Something like

 var div = ...some html   
 $("#mydiv").append(div);
 $(my new div).find(".subDiv").hide(); 
 $(my new div).data('user',object);

But how should I get the the new div I created? Is there a way to create it and then preform these actions, and then append it? Or should I append it and then retrieve it and modify it?

Efficiency is important as this will be iterated for search results...

Thanks!

I used this as my solution thanks to Tricker:

var div = ...a lagre piece of html;
var newDiv = $(div);
newDiv.find("[show='contractor']").hide();
newDiv.data('user', userObject);
$(appendDiv).append(newDiv);

Thanks!

Share Improve this question edited Dec 29, 2010 at 15:53 kralco626 asked Dec 29, 2010 at 14:20 kralco626kralco626 8,66441 gold badges115 silver badges171 bronze badges 2
  • you can chain function calls in jQuery, and you really should read the jQuery API as the appendTo function would have suited your needs better. The entire thing could have been written as: $(div).data('user', userObject).appendTo(appendDiv).find('[show="contractor"]').hide(); – zzzzBov Commented Dec 29, 2010 at 16:16
  • does appendTo return the parent or child object? I looked in the API and I can't find it. However, this could be written as $(appendDiv).append($(div).data('user',userObject).find("[show-'contractor']").hide()); I think, and that would be the same thing? – kralco626 Commented Dec 29, 2010 at 18:04
Add a ment  | 

3 Answers 3

Reset to default 4

The way u want is like this:

var new_obj = $('<div class="subDiv"></div>');

$("#my_div").append(new_obj);

//You dont have to find the subdiv because the object "new_obj" is your subDiv
new_obj.hide();
new_obj.data('user',object);

Does .append() not return the new item?

var myNewDiv= $("#mydiv").append(div);

since it will always be the last child div, I believe you can access it via $("#myDiv div:last-child")

发布评论

评论列表(0)

  1. 暂无评论