[英]How can I add line numbers to a div to make it looks like an IDE editor if that div not containing text, instead it contains floating HTML elements?
I am trying to add a line number for each line in a div. 我正在尝试为div中的每一行添加行号。 But my div body is just a floating combination of other HTML elements that are generated using js and injected to that div.
但是我的div主体只是使用js生成并注入到该div中的其他HTML元素的浮动组合。 I know that if I want to add line numbers to a div that contains a text or fixed with elements I can do this .
我知道,如果要将行号添加到包含文本或使用元素固定的div中,可以执行此操作 。 but How to do that for floating elements.
但是如何对浮动元素执行此操作。
var field = document.getElementById('field'); function randomIntFromInterval(min, max) { // min and max included return Math.floor(Math.random() * (max - min + 1) + min); } for (var i = 0; i < 100; i++) { var element = document.createElement('div'); if(randomIntFromInterval(1, 3) == 1) element.classList.add("child_1_div"); else if(randomIntFromInterval(1, 3) == 2) element.classList.add("child_2_div"); else if(randomIntFromInterval(1, 3) == 3) element.classList.add("child_3_div"); field.appendChild(element); }
.parent_div { width: 500px; height: 500px; font-size: 0; border: 1px solid #FF0000; } .child_1_div { font-size: 1rem; display: inline-block; width: 30%; height:20px; box-sizing: border-box; border: 1px solid #000; } .child_2_div { font-size: 1rem; display: inline-block; width: 10%; height: 10px; box-sizing: border-box; border: 1px solid #000; } .child_3_div { font-size: 1rem; display: inline-block; width: 5%; height: 5px; box-sizing: border-box; border: 1px solid #000; }
<div id ="field" class="parent_div"></div>
I think it's a little difficult to give an answer not knowing how you want it to look. 我认为要给出答案可能有点困难,因为它不知道您想要它的外观。 A screenshot would have helped.
屏幕截图会有所帮助。 However I assume you just want any text beside each of the divs even if they are floated, to do that you would simply add this CSS:
但是我假设您只希望每个div旁边的任何文本(即使它们是浮动的)也可以,只需添加以下CSS:
#field {
/* Set "my-sec-counter" to 0 */
counter-reset: my-sec-counter;
}
#field div::before {
/* Increment "my-sec-counter" by 1 */
counter-increment: my-sec-counter;
content: "Section " counter(my-sec-counter) ". ";
float:left;
}
#field div {
font-size: 1rem;
}
var field = document.getElementById('field'); function randomIntFromInterval(min, max) { // min and max included return Math.floor(Math.random() * (max - min + 1) + min); } for (var i = 0; i < 100; i++) { var element = document.createElement('div'); if(randomIntFromInterval(1, 3) == 1) element.classList.add("child_1_div"); else if(randomIntFromInterval(1, 3) == 2) element.classList.add("child_2_div"); else if(randomIntFromInterval(1, 3) == 3) element.classList.add("child_3_div"); field.appendChild(element); }
.parent_div { width: 500px; height: 500px; font-size: 0; border: 1px solid #FF0000; } .child_1_div { font-size: 1rem; display: inline-block; width: 30%; height:20px; box-sizing: border-box; border: 1px solid #000; } .child_2_div { font-size: 1rem; display: inline-block; width: 10%; height: 10px; box-sizing: border-box; border: 1px solid #000; } .child_3_div { font-size: 1rem; display: inline-block; width: 5%; height: 5px; box-sizing: border-box; border: 1px solid #000; } #field { /* Set "my-sec-counter" to 0 */ counter-reset: my-sec-counter; } #field div::before { /* Increment "my-sec-counter" by 1 */ counter-increment: my-sec-counter; content: "Section " counter(my-sec-counter) ". "; float:left; } #field div { font-size: 1rem; }
<div id ="field" class="parent_div"></div>
Upon reading the comments it seems you need line numbers as in when you open a code editor. 阅读注释后,似乎您需要像打开代码编辑器一样的行号。 To do that you would need to create a new div.
为此,您需要创建一个新的div。 Float that div to the left so that it is always beside your #field div.
将div浮动到左侧,使其始终在#field div旁边。 Then in the new div add numbers which are floated left and clear float left so that each number is on the next line.
然后在新的div中添加向左浮动的数字,并清除向左浮动的数字,以便每个数字都在下一行。 Something like this:
像这样:
<div id="lineNumbers">
<span>1</span>
<span>2</span>
<span>3</span>
</div>
#lineNumbers {
width: 20px;
float:left;
border:1px solid green;
}
#lineNumbers span {
display:inline-block;
float:left;
clear:both;
}
example with jQuery from my comment 我的评论中带有jQuery的示例
you could check each element left position inside the container , if it is eqaul to 0, then add a class to generate a pseudo elemnt.
您可以检查容器内每个元素的左侧位置(如果eqaul等于0,然后添加一个类以生成伪元素)。 CSS counter can be used and incremented on that class.
CSS计数器可以在该类上使用并递增。
example trying with jquery. 示例尝试使用jQuery。
function testme() { $('#field.parent_div').children().each(function() { var $currentElement = $(this); if ($currentElement.position().left === 0) { $currentElement.addClass(" lines") } }); } var field = document.getElementById("field"); function randomIntFromInterval(min, max) { // min and max included return Math.floor(Math.random() * (max - min + 1) + min); } for (var i = 0; i < 100; i++) { var element = document.createElement("div"); element.classList.add("child_div");// you mist a few if (randomIntFromInterval(1, 3) == 1) element.classList.add("child_1_div"); else if (randomIntFromInterval(1, 3) == 2) element.classList.add("child_2_div"); else if (randomIntFromInterval(1, 3) == 3) element.classList.add("child_3_div"); field.appendChild(element); }
.parent_div { counter-reset: line; } .parent_div div.lines { counter-increment: line; /* see me */ background: lightblue; } .parent_div .lines::before { content: counter(line); position: absolute; right: 100%; margin-right: 0.5em; background: yellow; } .parent_div { width: 500px; height: 500px; font-size: 0; border: 1px solid #ff0000; position: relative; margin-left: 2rem; } .child_div { display: inline-block; width: 2rem; height: 1rem; font-size: 1rem; border: solid 1px; } .child_1_div { font-size: 1rem; display: inline-block; width: 30%; height: 20px; box-sizing: border-box; border: 1px solid #000; } .child_2_div { font-size: 1rem; display: inline-block; width: 10%; height: 15px; box-sizing: border-box; border: 1px solid #000; } .child_3_div { font-size: 1rem; display: inline-block; width: 5%; height: 25px; box-sizing: border-box; border: 1px solid #000; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button onclick="testme();">show line numbers</button> <div id="field" class="parent_div"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.