Jquery在标签内部获取所有内容

时间:2022-11-03 10:07:24

I'm working on a project and I'm trying to get everything from inside a p-tag.

我正在研究一个项目,我正试图从p-tag内部获取所有内容。

Let's say the HTML code is this

我们说HTML代码是这样的

<p>Hey everyone, this is it</p>

And JQUERY:

$('p').each(function(i) {
      ( $(this).html());
});

This.html() now contains the html inside the paragraph, OK good.

This.html()现在包含段落中的html,好的。

If I have <p>Hey...<a href='something'>link</a></p> everything is still available through this.html(). Or I could get the <a>-contents from this.children.

如果我有

嘿... 链接 通过this.html()仍然可以获得所有内容。或者我可以从this.children获得 -contents。

BUT if I have a <div> or a <li> or <ul> tags inside my <p>, this.html() does not contain the div or li or ul, and neither does this.children. This.html() stops before the tag.

但是如果我的

中有

    • 标签,则this.html()不包含div或li或ul,并且this.children也不包含。 This.html()在标记之前停止。

  • 或标签,则this.html()不包含div或li或ul,并且this.children也不包含.This.html()在标记之前停止。

    How do I retrieve EVERYTHING from my <p>-tag no matter what tags are in there?

    无论哪个标签在哪里,我如何从

    -tag中检索所有内容?

    3 个解决方案

    #1


    1  

    You should switch to use <div> tag instead of <p>. Then you won't have this issue.

    您应该切换到使用

    标签而不是

    。那你就不会有这个问题。

    #2


    0  

    Yeah wrap everything in a div rather than <p> <p> can only have certain elements in it such as links, strong, bold, spans <div> can contain anything and is also a block level element

    是的,在div中包装所有内容而不是

    只能在其中包含某些元素,例如链接,强,粗,spans

    可以包含任何内容,也是块级元素

    #3


    0  

    When you put a div inside a p, the browser automatically ends the paragraph and starts a division. It is as if instead of <div>, ther was </p></div>. The same happens if you start a new paragraph.

    当你将div放在p中时,浏览器会自动结束段落并开始除法。这好像不是

    ,而是 。如果你开始一个新段落也会发生同样的情况。

    #1


    1  

    You should switch to use <div> tag instead of <p>. Then you won't have this issue.

    您应该切换到使用

    标签而不是

    。那你就不会有这个问题。

    #2


    0  

    Yeah wrap everything in a div rather than <p> <p> can only have certain elements in it such as links, strong, bold, spans <div> can contain anything and is also a block level element

    是的,在div中包装所有内容而不是

    只能在其中包含某些元素,例如链接,强,粗,spans

    可以包含任何内容,也是块级元素

    #3


    0  

    When you put a div inside a p, the browser automatically ends the paragraph and starts a division. It is as if instead of <div>, ther was </p></div>. The same happens if you start a new paragraph.

    当你将div放在p中时,浏览器会自动结束段落并开始除法。这好像不是

    ,而是 。如果你开始一个新段落也会发生同样的情况。