jquery在div中添加id,并使用id进行操作 [英] jquery append div inside div with id and manipulate

查看:121
本文介绍了jquery在div中添加id,并使用id进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个div:

 < div id =box>< / div> 

我想在上面的div中使用jquery添加div。



我试过了(下面的代码在函数中):

  var e = $('< div style =display:block; float:left; width:'+ width +'px; height:'+ height +'px; margin-top:'+ positionY +'px; margin-left:'+ positionX +'px; border: 1px虚线#CCCCCC;>< / div>'); 
$('div',e).attr('id','myid');
$(#box)。append(e);

但访问$(#myid)无法正常工作。



有关如何在div中添加div并能够操作它们的任何想法?



谢谢!

解决方案

这只是错误的顺序

  var e = $('< div style =display:block; float:left; width:'+ width +'px; height:'+ height +'px; margin-top:'+ positionY +'px; margin-left:'+ positionX + 'px; border:1px dashed #CCCCCC;>< / div>'); 
$('#box')。append(e);
e.attr('id','myid');

先追加,然后访问/设置attr。


after 2 hours of searching I decided to ask my question.

I have a div:

<div id="box"></div>

I want to add a div inside the above div using jquery.

I tried (following code is inside a function):

var e = $('<div style="display:block; float:left;width:'+width+'px; height:'+height+'px; margin-top:'+positionY+'px;margin-left:'+positionX+'px;border:1px dashed #CCCCCC;"></div>');
$('div', e).attr('id', 'myid');
$("#box").append(e);

but accessing $("#myid") is not working.

any idea on how to add a div inside a div and be able to manipulate them?

thank you!

解决方案

It's just the wrong order

var e = $('<div style="display:block; float:left;width:'+width+'px; height:'+height+'px; margin-top:'+positionY+'px;margin-left:'+positionX+'px;border:1px dashed #CCCCCC;"></div>');
$('#box').append(e);    
e.attr('id', 'myid');

Append first and then access/set attr.

这篇关于jquery在div中添加id,并使用id进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆