简体   繁体   English

在特定元素后面换行<p>

[英]Wrap text following a particular element with <p>

I've got some HTML like this: 我有一些这样的HTML:

<div class="text">
    <img src="http://placehold.it/50x50"/><a href="http://www.example.com" class="ht">the link</a>
    this element doesn't look right, the text displays next to the link
</div>
<div class="text">
    <img src="http://placehold.it/50x50"/><a href="http://www.example.com" class="ht">the link</a> 
    <p>this is a proper kind of element, i want them wrapped in paragraphs or at least looking like they've been wrapped in paragraphs</p>
</div>

and I was wondering if it would be possible to make the text from the first example <div> display below the link, like wrapping a <p></p> around the text, with jQuery. 我想知道是否有可能使第一个示例<div>的文本显示在链接下方,例如使用jQuery将<p></p>环绕在文本周围。 It's not possible to change the HTML because it's a user-generated comments kind of project, and I can't exactly force the user to put <p></p> around their text. 不能更改HTML,因为它是用户生成的注释类型的项目,而且我不能完全强迫用户在其文本周围放置<p></p> Most of them do, but some of them don't so I need to find a solution to make them all display the same. 它们大多数都可以,但是有些却不,所以我需要找到一种解决方案以使它们全部显示相同。

I've done some research and I found this answer: Selecting text only following certain elements using javascript/jquery but it doesn't really answer my question as that's based on a particular fixed structure of text and what I have as text can really be anything the user inputs and I need it to display below the image and the link, which display on the same line. 我做了一些研究,发现了这个答案: 仅使用javascript / jquery在某些元素后面选择文本,但这并不能真正回答我的问题,因为这是基于特定的固定文本结构,而我拥有的文本确实可以用户输入的所有内容,我需要它显示在图片和链接下方,并显示在同一行上。 This solution: Wrap text after particular symbol with jQuery also doesn't work for me because there is no set HTML that I can use to identify the unwrapped text. 此解决方案: 使用jQuery将特定符号后的文本换行对我也不起作用,因为没有可用于标识未包装文本的设置HTML。 This one: Find text in element that is NOT wrapped in html tags and wrap it with <p> also doesn't do what I need because sometimes we have people using <b></b> and <i></i> 这一个: 在未包裹在html标记中的元素中查找文本,并用<p>包裹也不能满足我的需要,因为有时我们有些人使用<b></b><i></i>

Other things I've tried: 我尝试过的其他方法:

  • Making the image and link have display:block; 使图像和链接具有display:block; and float:left; float:left; which doesn't work because the text still displays on the same line as the image and link 这不起作用,因为文本仍与图像和链接显示在同一行上
  • Making the image and link have display:block; 使图像和链接具有display:block; without the float - doesn't work because the image and link don't display on the same line anymore 没有浮点数-不起作用,因为图像和链接不再显示在同一行上
  • Using $("a.ht").next() to select, which just doesn't work 使用$("a.ht").next()进行选择,但这是行不通的

So if anyone could help me out with this I'd be really grateful, thank you! 因此,如果有人可以帮助我,我将非常感谢,谢谢!

EDIT: Here's an example of what I want: 编辑:这是我想要的一个示例:

try something like: 尝试类似的东西:

 $('.text').each(function() { var el = $(this).clone().find('img,.ht');//get the image and the title var text = el.remove().end().html();//remove the elements, get the remaining html $(this).html(el).append('<p>' + text + '</p>');//add the p with the remaining html }); 
 p {color:red;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="text"> <img src="http://placehold.it/50x50" /><a href="http://www.example.com" class="ht">the link</a> this element doesn't look right, the text displays next to the link </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="text"> <img src="http://placehold.it/50x50" /><a href="http://www.example.com" class="ht">the link</a> this element doesn't look right, the <b>text displays next</b> to the <i>link</i> </div> 

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

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