jquery - 从子 div 中追加父 div

标签 jquery html css

我想在 div 1 之前添加一个 div,但脚本将嵌套在代码深处的几个 div 中。这在理论上可行吗?

<div id="1">
   <div id="2">
       <div id="3">
           <style>
           #4 {...}
           </style>
           <script src="//code.jquery.com/jquery-1.10.2.js"></script>
           <script>     
               $( "#4" ).before( $( "#1" ) );
           </script>
       </div>
   </div>
</div>

谢谢

最佳答案

这应该在 id 为 1 的 div 之前注入(inject)一个 div 元素。

<div id="1">
   <div id="2">
       <div id="3">
           <style>
           </style>
           <script src="//code.jquery.com/jquery-1.10.2.js"></script>
           <script>
             var $div = $('<div></div>').attr('id', '4'); 

             $div.insertBefore('#1');
           </script>
       </div>
   </div>
</div>

关于jquery - 从子 div 中追加父 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35045878/

相关文章:

jquery - 使用 jQuery 实时更新文本

html - 固定页眉和页脚

javascript - FileReader 的 onloadend 事件永远不会被触发

c# - JavaScript 运行时错误 :function expected ajax call

html - 为什么 z-index 虽然指定了 "position"却不起作用?

php - 如何以特定的窗口宽度显示 HTML 元素?

css - EXTjs5 - 自定义主题问题 1 [Tab]

jquery - 为什么是:target pseudo class not working in IE?

javascript - 制作 Disqus 2012 RTL

html - 使 NavItem 背景颜色与 NavBar 相同