CSS - 抓取元素并内联显示它们(绝对定位?)

标签 css position inline absolute

我有一个充满 DOM 元素的网页,我想从该页面中取出所有 h3 并将它们并排显示在页面顶部。问题是 - 这可能吗?

<h3>I want</h3>
    There are tons of other content between them so...
<h3>These headers</h3>
    Keep in mind the're separated by many DOM elements...
<h3>To be displayed</h3>
    Luckily no other h3s between them...
<h3>Inline next to</h3>
    Thank you for trying to help :)
<h3>Each other </h3>

这是 jsfiddle,我尝试使用绝对定位,但我很确定采用这种方式会很困难(边距):

http://jsfiddle.net/zLbuP/

我需要代码至少适用于 IE7 及以上版本,我不能使用任何 JS/jQuery(不过使用 jQuery 会很容易) 。当然我也无法编辑html本身

有什么想法,或者不可能吗? ;)

最佳答案

使用 jQuery/Javascript 很简单吗?

我只是乱搞了一下,想出了这个:http://jsfiddle.net/zLbuP/19/

代码只是从所有 H3 中获取内容,删除它们并创建一个新的组合 H3。

//When then DOM is ready
jQuery(document).ready(function($) {
    //Cache the content of the headings to a variable so it may be removed before creating a new H3
    //this allows us to select the new H3 without having to use more complex selectors
    var h3text = $('#WhatIHaveNow h3').text();
    // remove the old H3's now that we have their content
    $('#WhatIHaveNow h3').remove();
    // create a new, empty H3 at the top of the parent div
    $('#WhatIHaveNow').prepend("<h3></h3>");
    //insert our cached content
    $('#WhatIHaveNow h3').text(h3text);

});

关于CSS - 抓取元素并内联显示它们(绝对定位?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14637128/

相关文章:

javascript - 在 CSS/JS 中模拟旧浏览器渲染的最佳方式?

html - 将背景图像以其原始宽度和高度的 50% 居中

css - 带渐变的圆形虚线边框

c++ - 内联函数,总是n+1份代码,其中 'n'是调用次数

css - 图像未内联显示

css - 少组织 :nth-child active states

javascript - 使用位置 : relative; 在图片区域内包含文本

html - 使固定的 div 在滚动的导航栏后面折叠

html - Bootstrap 4 设置输入组输入的宽度

html - 背景 Chrome 中的无效属性值