我想要显示一些文本,分成给定长度的组,但不是retain the ability to search the page for text that spans multiple groups.有了这个,我想在每个组上面显示一些字符.

我最终得到的解决方案是以下一个:

  body {
    margin-top: 1rem;
    font-family: mono;
  }

  br {
    margin-bottom: 2rem;
  }

  span {
    margin-right: 1rem;
    position: relative;
  }

  .absolute-before::before {
    content: "⇣";
    position: absolute;
    top: -1.2rem;
    left: 0;
    color: red;
  }

  .static-before::before {
    content: "⇣";
    color: green;
  }
   <span class="absolute-before">This_is_a_sp</span><span class="absolute-before">lit_string</span>

   <br />

   <span class="static-before">This_is_a_sp</span><span class="static-before">lit_string</span>

Note: the string and split position are hard-coded here, but are dynamically calculated in the real case.

The first one (red arrows) makes the visual part of the job is perfectly fine, but the problem is that, in Firefox (tested in Chromium, where it works), searching the page for the text split, which is split between the two groups, gives no results.
The second one (green arrows), conversely, does not give a good visual but the search feature works well.
The difference is in the position: absolute on the ::before in the first one, which seems to break the search feature.

你对如何做到这一点有什么线索吗?我不确定实现这一点的方式(特别是::before方式),所以欢迎任何 idea !唯一的要求是使用浏览器的原生Search in page...工具.

Edit:

我发现Bugzilla bug也报告了这个问题.因此,使用::before方法😪似乎不太可能解决这个问题.仍然欢迎任何其他解决方案!

推荐答案

我之前的答案是将文本内容合并到一个组中,以便在任何地方都能进行本地搜索.

但最近另一位用户(@LS_)给出的答案启发了我采取不同的方法.你仍然可以继续使用不同的跨度,对内容进行分组,同时对连接的文本进行工作搜索.

这里的诀窍是在新的div相对于整个容器绝对定位时创建这些箭头.这样的策略不会在组之间插入文本内容,并将保留搜索,就像文本是连续的一样.

Recap:

这段代码包括一个包含<div class="container">个包装<span class="fragment">个项目的HTML.在页面加载时,Java脚本将负责在这些span.fragment中的每一个的顶部创建箭头.文本将是一个很好的搜索目标,就像那些片段属于一个连续的文本一样.Attention!!个相邻的片段将被认为是一个单独的单词,不被空格分隔,只要这些跨度在同一行上,没有换行符!

//on page ready
document.addEventListener('DOMContentLoaded', ()=>{  
  //this is the container target
  //NOTE! I'm using querySelector on a classname.. it will return only the first element having that class
  const containerDiv = document.querySelector('.container');
  
  //it draws the arrows inside the target on top of each children span
  redrawArrows(containerDiv);
  
  //sets up an observer that will redraw the arrows when the target container is resized
  const resizeObserver = new ResizeObserver( ()=>{
   //using a delay of 50ms before redrawing (to be 100% sure it won't mess up with coords too early)
   setTimeout(() => {
        redrawArrows(containerDiv);
    }, 50);    
  } );
  resizeObserver.observe(containerDiv);
})

function redrawArrows(container){  

  //remove any existing arrows
  container.querySelectorAll('.arrow')
    .forEach(arrow => arrow.remove());

  //for each .fragment in container, adds an arrow in top
  container.querySelectorAll('.fragment')
    .forEach(fragment => addArrowOnTopOfSpan(container, fragment));
}

function addArrowOnTopOfSpan(container, span){  
  document.body.offsetHeight; // Force reflow
  
  //creates a new div to render the arrow
  const arrow = document.createElement('div');
  arrow.classList.add('arrow');    
  arrow.innerHTML = '⇣';    
  arrow.style.left = `${span.offsetLeft}px`;
  //the y coord is shifted 1em upward
  arrow.style.top = `calc( ${span.offsetTop}px - 1em )`;    
  
  container.appendChild(arrow);
}
body {
  margin-top: 1rem;
}

.container{
  position: relative;
}

.fragment{
  margin-right: 1em;
  margin-top: 1em;
  /*this is important for the offset coords to work as expected!*/
  display: inline-block;
}

.arrow{
  position: absolute;
  color: red;
}

.note{
  margin-top: 2em;
}
<div class="container">
  <span class="fragment">This_is_a_very_long_text_to</span><span class="fragment">highlight_a_scenario_where</span><span class="fragment">the_content_will_word_wrap</span><span class="fragment">no_matter_what_even_if_theres</span><span class="fragment">no_space_in_between_words</span>
</div>

Html相关问答推荐

值更新时旋转数字动画

Rmarkdown上的垂直标签集

springBoot + Thymeleaf:基于Locale设置页面语言

将网格包装在css中

将2个Flex列合并为1个交替子列

使用HTML进行DAX测量

天使17:令人惊叹的动画

我怎样才能在我的网站上制作花哨的角落

如何使用Reaction Js读取html文件中的代码

带有下拉菜单的完整css导航-链接不起作用?

如何使用css横向显示英文和中日韩文字?

浮动Div在CSS中未按预期工作

如何使用*ngFor将模板从父级传递到子级

Swift 以编程方式在 YouTube 嵌入视频中进入全屏

Div 容器不会遵守边距、填充或间隙

为什么当我按第三个按钮时,前两个按钮会跳动?

在 laravel 中使用 DomPDF 将数据导出为 pdf 时,第二列被拉伸

在Firefox中使用keySplines时,SVG: <animateMotion>不起作用

屏幕缩小时背景图像裁剪高度

我正在创建一个 django 审查系统.我认为我的 HTML 是错误的