javascript - 在单独层次结构的两个元素之间显示 <div>

标签 javascript jquery html css

考虑以下 HTML:

<div style="background-color: blue">
    <div style="color: red">
        some content some content some content some content 
    </div>
</div>

<div id="highlight" style="top: 0px; width: 200px; height: 40px; 
                           background-color: yellow; position: fixed">
</div>

on jsfiddle


看起来像这样:

Current result

我想做的是在视觉上放置 #highlight <div>在蓝色背景和文本之间。这是我想要的样子:

Desired result


我正在使用这个奇怪的 <div>在我的现实生活场景中的层次结构我正在使用 JQuery 移动 #highlight一些鼠标/键盘事件。我也无法控制 <div> 的层次结构s 包含文本。

我需要 #highlightposition要么是fixedabsolute .

有什么方法可以在不更改 HTML 结构的情况下实现当前结果?我尝试使用 z-index ,但它不起作用,因为层次结构优先于它。我对任何涉及 JavaScript/JQuery 的肮脏黑客行为持开放态度。

最佳答案

在内容div上,设置z-index:1position:relative

<div style="background-color: blue;">
  <div style="color: red; z-index: 1;position:relative;">
    some content some content some content some content
  </div>
</div>

<div id="highlight" style="top: 0px; width: 200px; height: 40px; background-color: yellow; position: fixed;">
</div>

关于javascript - 在单独层次结构的两个元素之间显示 <div>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47160504/

相关文章:

javascript - 如何在没有ajax的情况下调用上一页

javascript - 如何重用ui-grid外部分页和排序方法?

javascript - 如何定位侧加载的 HTML

javascript - OO Javascript - 正确的范围处理?

html - 非输入的 Bootstrap 输入组插件图标

html - 除非选中复选框,否则禁用按钮(Angular 7+)?

javascript - 如何将图像位置放入 django 模板中的 javascript 代码中

Java Play 2.5 中的 Javascript 路由?

javascript - 背景图像事件处理程序

javascript - 无法通过 javascript 从输入字段中选取值