简体   繁体   English

仅使用CSS / JS更改元素的位置

[英]Change position of an element using CSS/JS only

I am using Uikit 2 ( https://getuikit.com/v2 ) - and I need to solve a problem. 我正在使用Uikit 2( https://getuikit.com/v2 )-我需要解决一个问题。

I got this markup: https://codepen.io/anon/pen/jZwNeB 我得到了这个标记: https : //codepen.io/anon/pen/jZwNeB

Now I need to do the following. 现在,我需要执行以下操作。 This part: 这部分:

<div class="toc uk-panel uk-panel-box-primary sticky-toc" style="margin: 0px;"> ...</div>

Should be shown on the left side - right under the time datetime part. 应该显示在时间datetime部分的左侧-右侧。 But - and this is important: I can not change the source itself. 但是-这很重要:我无法更改来源本身。 I can only add a class to toc uk-panel uk-panel-box-primary sticky-toc and add custom CSS and custom JS. 我只能将一个类添加到toc uk-panel uk-panel-box-primary sticky-toc并添加自定义CSS和自定义JS。 Any idea how to solve this? 任何想法如何解决这个问题?

var obj = document.getElementById("node");
var parent = document.getElementById("parent");
parent.appendChild(obj);

Here, node is the "toc uk-panel uk-panel-box-primary sticky-toc" element. 在此,节点是“ toc uk-panel uk-panel-box-primary sticky-toc”元素。 parent is the "uk-width-large-1-4" element 父级是“ uk-width-large-1-4”元素

You can obviously use any other DOM method than the one I have used. 显然,您可以使用除我使用过的其他任何DOM方法。 So, if you want to select the DOM of the entity using its class name class, you have to use getElementsByClassName("big long class name")[0] to correctly reference that entity 因此,如果要使用其类名class选择实体的DOM,则必须使用getElementsByClassName(“ big long class name”)[0]正确引用该实体

I just wanted to highlight the appendChild method 我只是想突出显示appendChild方法

I believe the best answer is using CSS, but because I don't know about getukit and I don't know how to solve this using CSS only. 我相信最好的答案是使用CSS,但是因为我不了解getukit,也不知道如何仅使用CSS来解决这个问题。

Here you can try with jQuery. 在这里,您可以尝试使用jQuery。 Tell me if you want do this using pure JS. 告诉我您是否要使用纯JS执行此操作。

 <link href='https://cdnjs.cloudflare.com/ajax/libs/uikit/2.27.5/css/uikit.min.css' rel='stylesheet'> <div class="uk-grid"> <div class="uk-width-large-1-4"> <p class="uk-article-meta tm-article-date uk-margin-small selectionShareable"> <time datetime="2018-02-12T12:00:58+00:00">12.02.18</time> </p> </div> <div class="uk-width-large-3-4"> <h1 class="uk-article-title">Test Content</h1> <div class="uk-margin"> <div class="uk-sticky-placeholder" style="height: 52px; margin: 0px;"> <div class="toc uk-panel uk-panel-box-primary sticky-toc" style="margin: 0px;"> <ol class="toc-list "> <li class="toc-list-item"> <a href="#" class="toc-link node-name--H1 is-active-link">Testa</a> <ol class="toc-list is-collapsible"> <li class="toc-list-item"><a href="#test" class="toc-link node-name--H2 ">Test 2</a></li> </ol> </li> </ol> </div> </div> <p class="selectionShareable">Test Content.</p> </div> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(function(){ function recreate(){ let newElem = $('.sticky-toc').clone(); $('.sticky-toc').remove(); $('.tm-article-date').after(newElem); } recreate(); }) </script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM