在 Javascript 中使用附加功能将一个 div 嵌套到另一个 div 中

发布于 2024-12-05 04:16:40 字数 2089 浏览 0 评论 0原文

我在这个简单的 javascript 中遇到困难,我想将一个 div 嵌套在另一个 div 中。 div 看起来像这样。

<div id="div01" class="comdiv ui-widget-content" style="position: absolute; top: 40px; left: 40px; width:350px; height:250px;">
    <p id="heading" class="comhdr editableText">Editable</p>
    <div class="toolbar">
             <a href='#' title='Options' class='icotools'></a>
             <a href='#' title='Delete' class='icodelete'></a>
    </div>
    <br/>
    <div class="link_drop_box">

      <!-- Nest div here -->

    </div>
</div>

在“link_drop_box”中我想嵌套这个div。

<div id="u0014" class="comurl" onmouseOver="url_preview('show', this, 'div01');" onmouseOut="url_preview('hide', this, 'div01');"><img class="dhandle" src="http://www.google.com/s2/favicons?domain=tf1.fr" align="middle" />&nbsp;<a href="http://tf1.fr" target="_blank">TF1.fr</a> <img class="urlbutton" title="Delete" src="/icodeact/Delete16.png" onClick="delete_url('u0014');"/> <img src="/images/spacer.png" class="spacer" /> <img class="urlbutton" title="Edit" src="/icodeact/Edit16.png" onClick=""/> 
</div>

我尝试用这个 javascript 将此 div 嵌套在前面的 div 中。

$('#'+card_id).find('.link_box_drop').append('<div id="'+link_id+'" class="link" onmouseOver="link_preview(\'show\', this, \''+card_id+'\');" onmouseOut="link_preview(\'hide\', this, \''+card_id+'\');"><img class="dhandle" src="http://www.google.com/s2/favicons?domain='+link_ico+'" align="middle" />&nbsp;<a href="'+link_info[1]+'" target="_blank">'+link_info[0]+'</a> <img class="link_button" title="Delete" src="/icodeact/Delete16.png" onClick="delete_url(\''+link_id+'\');"/> <img src="/images/spacer.png" class="link_button_spacer" /> <img class="link_button" title="Edit" src="/icodeact/Edit16.png" onClick=""/> </div>');

我认为使用 .link_drop_box 选择器我可以使用 find 函数获取容器 div,然后追加。在该容器内附加元素的正确方法是什么?

I am having difficulty with this simple javascript where I want to nest a div inside another div. The div looks like this.

<div id="div01" class="comdiv ui-widget-content" style="position: absolute; top: 40px; left: 40px; width:350px; height:250px;">
    <p id="heading" class="comhdr editableText">Editable</p>
    <div class="toolbar">
             <a href='#' title='Options' class='icotools'></a>
             <a href='#' title='Delete' class='icodelete'></a>
    </div>
    <br/>
    <div class="link_drop_box">

      <!-- Nest div here -->

    </div>
</div>

In the "link_drop_box" I want to nest this div.

<div id="u0014" class="comurl" onmouseOver="url_preview('show', this, 'div01');" onmouseOut="url_preview('hide', this, 'div01');"><img class="dhandle" src="http://www.google.com/s2/favicons?domain=tf1.fr" align="middle" /> <a href="http://tf1.fr" target="_blank">TF1.fr</a> <img class="urlbutton" title="Delete" src="/icodeact/Delete16.png" onClick="delete_url('u0014');"/> <img src="/images/spacer.png" class="spacer" /> <img class="urlbutton" title="Edit" src="/icodeact/Edit16.png" onClick=""/> 
</div>

I try to nest this div in the previous with this javascript.

$('#'+card_id).find('.link_box_drop').append('<div id="'+link_id+'" class="link" onmouseOver="link_preview(\'show\', this, \''+card_id+'\');" onmouseOut="link_preview(\'hide\', this, \''+card_id+'\');"><img class="dhandle" src="http://www.google.com/s2/favicons?domain='+link_ico+'" align="middle" /> <a href="'+link_info[1]+'" target="_blank">'+link_info[0]+'</a> <img class="link_button" title="Delete" src="/icodeact/Delete16.png" onClick="delete_url(\''+link_id+'\');"/> <img src="/images/spacer.png" class="link_button_spacer" /> <img class="link_button" title="Edit" src="/icodeact/Edit16.png" onClick=""/> </div>');

I thought that with the .link_drop_box selector I could get the container div with the find function, then append. What is the right way to append an element inside that container?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╄→承喏 2024-12-12 04:16:40

您的 HTML 包含:

<div class="link_drop_box">

您的 JQuery 代码:
$('#'+card_id).find('.link_box_drop'),应为:
$('#' +card_id).find('.link_drop_box')

Your HTML contains:

<div class="link_drop_box">

Your JQuery code:
$('#'+card_id).find('.link_box_drop'), which should be:
$('#'+card_id).find('.link_drop_box').

星星的軌跡 2024-12-12 04:16:40
$('#div01 div.link_drop_box:first-child').append("<div id=\"u0014\" class=\"comurl\" onmouseOver=\"url_preview(\'show\', this, \'div01\');\" onmouseOut=\"url_preview(\'hide\', this, \'div01\');\"><img class=\"dhandle\" src=\"http://www.google.com/s2/favicons?domain=tf1.fr\" align=\"middle\" /> <a href=\"http://tf1.fr\" target=\"_blank\">TF1.fr</a> <img class=\"urlbutton\" title=\"Delete\" src=\"/icodeact/Delete16.png\" onClick=\"delete_url('u0014');\"/> <img src=\"/images/spacer.png\" class=\"spacer\" /> <img class=\"urlbutton\" title=\"Edit\" src=\"/icodeact/Edit16.png\" onClick=\"\"/></div>");

这就能解决问题

$('#div01 div.link_drop_box:first-child').append("<div id=\"u0014\" class=\"comurl\" onmouseOver=\"url_preview(\'show\', this, \'div01\');\" onmouseOut=\"url_preview(\'hide\', this, \'div01\');\"><img class=\"dhandle\" src=\"http://www.google.com/s2/favicons?domain=tf1.fr\" align=\"middle\" /> <a href=\"http://tf1.fr\" target=\"_blank\">TF1.fr</a> <img class=\"urlbutton\" title=\"Delete\" src=\"/icodeact/Delete16.png\" onClick=\"delete_url('u0014');\"/> <img src=\"/images/spacer.png\" class=\"spacer\" /> <img class=\"urlbutton\" title=\"Edit\" src=\"/icodeact/Edit16.png\" onClick=\"\"/></div>");

this will do the trick

残疾 2024-12-12 04:16:40
$('div.link_box_drop', $('#'+card_id))

该选择器将在“card_id”div 的上下文中返回“link_drop_box”。

$('div.link_box_drop', $('#'+card_id))

This selector will return "link_drop_box" in the context of the "card_id" div.

于我来说 2024-12-12 04:16:40

您正在使用 jQuery。 是的,jquery 只是一个 js 库,但正如您所言在尝试将常规 js 与 jQuery 交织在一起时,您会遇到很多问题。

使用 jQuery 函数 .find()、.append() 等,您通常(如果不是总是)需要在类或 id 之前添加标签名称。前任。 .find('div.link_box_drop'),或

查看 jQuery 文档中的 find() 和 < a href="http://api.jquery.com/append/" rel="nofollow">append()

顺便说一句,在一行中阅读这么多代码确实很困难。 :-/

You're using jQuery. Yes, jquery is just a js library, but as you're doing, you'll run into plenty of problems trying to intertwine regular js with jQuery.

With the jQuery functions .find(), .append() etc, you usually, if not always, need the tag name in there before the class or id. Ex. .find('div.link_box_drop'), or

Look over the jQuery documentation for find() and append()

Btw, it's really hard to read that much code on one line. :-/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文