简体   繁体   English

如果该div不包含文本,而是包含浮动HTML元素,如何在div中添加行号以使其看起来像IDE编辑器?

[英]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.

相关问题 如何创建一个看起来像漂浮在 div 上的表单? - How do I create a form that looks like it's floating on a div? 如何在 Angular 中将 html 标签添加到 Quill 文本编辑器的内容 div? - How can I add html tag to the content div of Quill text editor in Angular? 如何使div文本逐行显示而不是一次全部显示? - How to make div text appear line by line instead of all at once? 使用JQuery,我该如何制作一个 <div> 出现,然后添加一些HTML,然后删除html然后隐藏div? - Using JQuery, how can I make a <div> appear, then add some html, then remove the html and then hide the div? 如何在DIV(带有DOM)中更改文本内容以添加SPAN元素? - How to change text content in DIV (with DOM) to add SPANs elements instead? jQuery:.html()替换div标签中的文本,但如何添加而不是替换? - Jquery: .html() substitute the text in a div tag, but how do I add to instead of substitute? 如何将粘贴的 HTML 代码转换为 contentedtiable div 中不包括特殊 HTML 元素的纯文本? - How can I transform pasted HTML code to plain text inside contentedtiable div excluding special HTML elements? 如何添加多个元素,例如<span>,</span> <p> <span>要么</span> <div> <span>内</span> <option> <span>的标签</span> <select> <span>在ReactJs中?</span> - How can I add multiple elements like <span>, <p> or <div> inside <option> tag of <select> in ReactJs? 如何在弹性背景上放置浮动的div并确保div保持在该位置? - How can I position a floating elastic div over an elastic background and make sure that the div remains in the position? 由于只有文本溢出,我该如何使长文本的div转到下一行? - How can I make a div with long text go to next line as it was only text-overflow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM