jquery 将 div 附加到 div 中并带有 id 并进行操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7911916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:26:44  来源:igfitidea点击:

jquery append div inside div with id and manipulate

jqueryhtml

提问by pelelive

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

经过2个小时的搜索,我决定提出我的问题。

I have a div:

我有一个 div:

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

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

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

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.

但访问 $("#myid") 不起作用。

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

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

thank you!

谢谢你!

回答by Moe Sweet

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.

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

回答by mu is too short

You're overcomplicating things:

你把事情复杂化了:

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>');
e.attr('id', 'myid');
$('#box').append(e);

For example: http://jsfiddle.net/ambiguous/Dm5J2/

例如:http: //jsfiddle.net/ambiguous/Dm5J2/

回答by jfriend00

Why not go even simpler with either one of these options:

为什么不使用以下任一选项更简单:

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

Or, if you want to append it to existing content:

或者,如果您想将其附加到现有内容:

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

Note: I put the id="myid"right into the HTML string rather than using separate code to set it.

注意:我将id="myid"权限放入 HTML 字符串中,而不是使用单独的代码来设置它。

Both the .html()and .append()jQuery methods can take a string of HTML so there's no need to use a separate step for creating the objects.

the.html().append()jQuery 方法都可以采用 HTML 字符串,因此无需使用单独的步骤来创建对象。

回答by coolguy

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