简体   繁体   English

使用jQuery附加后,子元素出现在AFTER之后而不是IN元素

[英]Child element appearing AFTER as opposed to IN the element when appended using jQuery

I am appending the following using jQuery: 我将使用jQuery附加以下内容:

var dateWrapper = '<p class="date">'+kendo.format('{0:dd MMMM yyyy}',new Date(date))+'<div class="triangle"></div></p>'
$('.wrapper').append(dateWrapper)

I want the triangle to appear inline or inline-block with the date. 我希望三角形与日期一起出现在行内或行内。 Once appended the DIV is placed after the P, and not inside. 附加后,DIV将放置在P后面,而不是内部。 Cannot fathom why this would happen. 无法理解为什么会发生这种情况。

You can not put div inside p tag. 您不能将div放在p标签内。 It will automatically put it outside the p tag. 它将自动将其放在p标签之外。 You should use.. 您应该使用..

var dateWrapper = '<p class="date">'+kendo.format('{0:dd MMMM yyyy}',new Date(date))+'<span class="triangle"></span></p>';
$('.wrapper').append(dateWrapper);

In css 在CSS

.triangle{
display:inline;
}

According to the HTML5 documentation ( and as far as i know it's the same for older versions ) , p accepts as children only pharsing content 根据HTML5文档(据我所知,旧版本是相同的), p作为子级仅pharsing content

p element p元素

Pharsing content means plain text or pharsing elements which include <a> , </span> , <strong> etc. but not <div> Pharsing content是指纯文本或pharsing elements ,包括<a></span><strong>等,但不包括<div>

pharsing content 逐步内容

So a structure like <p><div></div></p> is not a valid HTML5 structure, so it is automatically converted into <p></p><div></div><p></p> . 因此, <p><div></div></p>结构不是有效的HTML5结构,因此会自动转换为<p></p><div></div><p></p>

In conclusion you CAN NOT nest div inside p and that's why you don't get the desired result. 总之,您不能将div嵌套在p ,这就是为什么无法获得所需结果的原因。 I suggest, in your case, change the triangle from div to span 我建议您将三角形从div更改为span

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

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