【问题标题】:How to position a html element relative to another html element using CSS or Javascript如何使用 CSS 或 Javascript 相对于另一个 html 元素定位 html 元素
【发布时间】:2019-01-11 09:16:55
【问题描述】:

有没有办法使用 CSS 或 Javascript 相对于另一个 html 元素定位一个 html 元素?请检查下面的代码,我尝试复制“second-element”的innerhtml并附加在“first-element”中,但这破坏了其他功能。所以我需要一些 CSS 或 JS 修复而不改变 HTML 结构。

<body>
<div id=“first-element”>
    <p>— Select course —</p>
    <!— I need the second-element here —>
</div>

<div id=“second-element”> 
    <ul>
        <li>Maths<li>
        <li>Science</li>
    </ul>
</div>
</body>

【问题讨论】:

    标签: javascript html css position css-position


    【解决方案1】:

    <div id="first-element">
      <p>— Select course —</p>
    </div>
    <div id="second-element">
      <ul>
        <li>Maths</li>
        <li>Science</li>
      </ul>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script>
    $(document).ready(function() {
    $('#second-element ul').appendTo('#first-element');
    });
    
    </script>

    【讨论】:

    • 此解决方案不适合我的情况,无论如何感谢您的建议。
    猜你喜欢
    • 2013-10-11
    • 2012-06-24
    • 2010-09-14
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多