简体   繁体   English

如果我们在一个p标签内放置一个div,Jquery html()方法不起作用?

[英]Jquery html() method not working if we place a div inside a p tag?

This is my HTML: 这是我的HTML:

  <div class="accor">
  </div>
  <div class="accordionContentTwoCol">
   <p class="noEdit">
     <div>   name : </div>
  </p>
 <div>

I need to find html content of accordionContentTwoCol div (i have access only to accor ). 我需要找到accordionContentTwoCol div的html内容(我只能访问accor )。
If i try to print html inside accordionContentTwoCol div like this: 如果我尝试在accordionContentTwoCol div中打印html,如下所示:

alert("html inside accordionContentTwoCol :"+$('.accor').next().html());

It gives output like this: 它提供如下输出:
在此输入图像描述

Though HTML inside accordionContentTwoCol is: 虽然accordionContentTwoCol中的HTML是:

<p class="noEdit">
         <div>   name : </div>
</p>

why that happens? 为什么会这样?

An authoritative place to look for allowed containment relations is the HTML spec. 查找允许的包含关系的权威位置是HTML规范。 See, for example, http://www.w3.org/TR/html4/sgml/dtd.html . 例如,参见http://www.w3.org/TR/html4/sgml/dtd.html It specifies which elements are block elements and which are inline. 它指定哪些元素是块元素,哪些元素是内联元素。 For those lists, search for the section marked "HTML content models". 对于这些列表,请搜索标记为“HTML内容模型”的部分。

For the P element, it specifies the following, which indicates that P elements are only allowed to contain inline elements. 对于P元素,它指定以下内容,表示P元素仅允许包含内联元素。

<!ELEMENT P - O (%inline;)*            -- paragraph -->

This is consistent with http://www.w3.org/TR/html401/struct/text.html#h-9.3.1 , which says that the P element "cannot contain block-level elements (including P itself)." 这与http://www.w3.org/TR/html401/struct/text.html#h-9.3.1一致,它说P元素“不能包含块级元素(包括P本身)。”

Why <p> tag can't contain <div> tag inside it? 为什么<p>标签里面不能包含<div>标签呢?

The markup you use is wrong. 你使用的标记是错误的。 The <div> cannot be nested with <p> Tag. <div>不能与<p>标签嵌套。

Try using 尝试使用

<div style="display: inline">

Otherwise, use <span> instead of <div> . 否则,请使用<span>而不是<div>

From the HTML 4.01 specification section 9.3.1 HTML 4.01规范第9.3.1节

The P element represents a paragraph. P元素代表一个段落。 It cannot contain block-level elements (including P itself ). 它不能包含块级元素 (包括P本身 )。

如何访问`<div> `标签内的`<p> `标签? </p></div><div id="text_translate"><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $("p div").click(function() { $(this).css({ "color": "#F00", "font": "bold 20px verdana", "background-color": "#0FF" }); });</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;p&gt;Hello&lt;div&gt; World!&lt;/div&gt;&lt;/p&gt;</pre></div></div><p></p><p> 当我单击“世界”文本时,什么也没有发生,当我检查元素时,它说:</p><pre class="lang-html prettyprint-override"> &lt;p&gt;Hello&lt;/p&gt; &lt;div&gt; World&lt;/div&gt; &lt;p&gt;&lt;/p&gt;</pre><p> 注意那些&lt;p&gt;标签。 怎么会这样?</p></div> - How to access a `<div>` tag inside a `<p>` tag?

暂无
暂无

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

相关问题 jQuery:使用insertAfter方法在div标签内创建p标签 - jQuery: Use insertAfter method to create p tag inside div tag 如何将jQuery动画对象放在HTML画布或Div标签内? - How Do I Place jQuery Animated Object Inside A HTML Canvas or Div Tag? 查找文本<p> div HTML/Jquery 中最近的一行 - Finding text of <p> closest row inside a div HTML/Jquery 如何访问`<div> `标签内的`<p> `标签? </p></div><div id="text_translate"><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $("p div").click(function() { $(this).css({ "color": "#F00", "font": "bold 20px verdana", "background-color": "#0FF" }); });</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;p&gt;Hello&lt;div&gt; World!&lt;/div&gt;&lt;/p&gt;</pre></div></div><p></p><p> 当我单击“世界”文本时,什么也没有发生,当我检查元素时,它说:</p><pre class="lang-html prettyprint-override"> &lt;p&gt;Hello&lt;/p&gt; &lt;div&gt; World&lt;/div&gt; &lt;p&gt;&lt;/p&gt;</pre><p> 注意那些&lt;p&gt;标签。 怎么会这样?</p></div> - How to access a `<div>` tag inside a `<p>` tag? 如何在同一 div 中使用 jquery hover 和 html 标签 - How to use jquery hover for a div and an html tag inside the same div CKEditor 4 将 html div 标签更改为 p 标签 - CKEditor 4 changes html div tag to p tag 使用Jquery在HTML中填充“p”标记 - Populating a “p” tag in HTML with Jquery 平滑滚动不适用于 p 标签内的标签 - Smooth scroll not working on a tag inside p tag div 或 p 标记在 flex box 包装内 - div or p tag inside of flex box wrapping jQuery自定义数据属性在div标记内不起作用 - jquery custom data attribute not working inside div tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM