简体   繁体   English

使用jQuery将文本内的图像水平对齐

[英]Horizontally align images inside text with jQuery

I have a div, which contains an article. 我有一个div,其中包含一篇文章。 I mean, it may contain text and images. 我的意思是,它可能包含文字和图像。 Something like this: 像这样:

Blah Blah Blah Blah    Blah Blah Blah Blah    Blah Blah Blah Blah
Blah Blah Blah Blah    Blah Blah Blah Blah    Blah Blah Blah Blah
Blah Blah Blah Blah    Blah Blah Blah Blah.
{IMAGE}
Blah Blah Blah Blah    Blah Blah Blah Blah    Blah Blah Blah Blah
Blah Blah Blah Blah    Blah Blah Blah Blah    Blah Blah Blah Blah.
{IMAGE}
Blah Blah Blah Blah    Blah Blah Blah Blah.

So, there may not be a div around every image. 因此,每个图像周围可能没有div。

But I want to horizontally align center the images with jQuery. 但是我想水平对齐图像与jQuery。 My idea is creating a div around every image (with jQuery) with a specific class. 我的想法是使用特定的类围绕每个图像(使用jQuery)创建一个div。

But I don't know how to do that in jQuery. 但是我不知道如何在jQuery中做到这一点。

How to do that? 怎么做? OR Is there any better way? 还是有更好的方法?

Edit: The content is downloaded from the feed of another website. 编辑:内容是从另一个网站的提要中下载的。 And I want to align the images center horizontally. 我想水平对齐图像。

No need to add div around img it can be done only by css 无需在img周围添加div,只能通过CSS完成

Try This fiddle 试试这个小提琴

Just use display:block and margin:auto 只需使用display:blockmargin:auto

you can do something like this : 您可以执行以下操作:

in your case : 在您的情况下:

$('.test').append($('#image1')); $(”。试验 ')附加($(' #图像1' ));

In jquery you can also use wrap method : http://api.jquery.com/wrap/ 在jquery中,您还可以使用wrap方法: http : //api.jquery.com/wrap/

Ex: $('img').wrap(' <div class="test"></div> '); 例如:$('img')。wrap(' <div class="test"></div> ');

jQuery to wrap all <img> tags with <div class="imageclass"> : jQuery用<div class="imageclass">包装所有<img>标签:

$('img').wrap('<div class="imageclass" />');

CSS to center the <div> : CSS以<div>为中心:

.imageclass { margin: 0 auto; }

Is your html something like this? 你的HTML是这样的吗?

<div id="container">
    <div id="column1">
        article text article text
        <img />
        article text article text
    </div>
    <div id="column2">
        article text article text
        <img />
        article text article text
    </div>
    <div id="column3">
        article text with no image
        article text with no image
        article text article text
    </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM