javascript – 使用jQuery使元素扩展到其父级之外

栏目: jQuery · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/32721681/making-an-element-extend-outside-its-parent-with-jquery

我正在使用一些标记,我们在外部容器中有许多html块/控件,这个容器基本上是一个具有最大宽度的基础行.

在某些情况下,我需要块来突破这个容器,延伸超出最大宽度并占据整个宽度,但在索引方面保持在原始位置.跟我在一起!这看起来如下:

<div class="row">
    <div><em>Regular block here</em></div>
    <div class="full-width"><em>Full width block here</em></div>
    <div><em>Regular block here</em></div>
</div>

实际上我想要的是如下:

<div class="row">
    <div><em>Regular block here</em></div>
</div>       
<div class="full-width"><em>Full width block here</em></div>
<div class="row">
    <div><em>Regular block here</em></div>
</div>

不幸的是我们无法在html中正确地执行此操作,因此jQuery看起来是我们最好的选择.

我试过做这样的事情,但没有用,因为它没有正确格式化html:

$('div.row > .full-width').each(function() {
    $(this).before('</div>').after('<div>');
});

任何人都有任何实现这一点的想法,而没有完全重写整个DOM,否则我将不得不重新应用一大堆绑定.

提前致谢.

DEMO

首先是 unwrap() .full-width元素.这将删除主.row,然后删除.row中的下一个和前一个元素.

$('div.row > .full-width').each(function() {
  var $rowDiv = $('<div class="row">');
  $(this).unwrap('.row');
  $(this).prev().wrap($rowDiv);
  $(this).next().wrap($rowDiv);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
    <div><em>Regular block here</em></div>
    <div class="full-width"><em>Full width block here</em></div>
    <div><em>Regular block here</em></div>
</div>

翻译自:https://stackoverflow.com/questions/32721681/making-an-element-extend-outside-its-parent-with-jquery


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Big Java Late Objects

Big Java Late Objects

Horstmann, Cay S. / 2012-2 / 896.00元

The introductory programming course is difficult. Many students fail to succeed or have trouble in the course because they don't understand the material and do not practice programming sufficiently. ......一起来看看 《Big Java Late Objects》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具