[英]How to get html tag's inner html in js/jquery?
For example i have: 例如我有:
<p class="question">
this is my paragraph
<p> And i'm inside
tag in question class
<h1> And this is my heading</h1>
</p>
</p>
the question is how can i get inner html of question class like below: 问题是如何获取如下所示的问题类的内部html:
this is my paragraph
<p> And i'm inside tag in question class
<h1> And this is my heading</h1>
</p>
There are 2 functions to get the inner html of an element 有2个函数来获取元素的内部html
.text()
.html()
.text() 。文本()
console.log($(".question").text());
.text()
will give the innertext of the element but not tags .text()
将提供元素的内部文本,但不会给出标签
.html() .html()
console.log($(".question").html());
.html()
function this will give the inner tags also. .html()
函数还将提供内部标签。
this is my paragraph <p> And i'm inside
tag in question class <h1> And this is my heading
</h1> </p>
In your case
.html()
would be fine在您的情况下
.html()
会很好
As per w3c docs : 根据w3c docs :
A p element's end tag may be omitted if the p element is immediately followed by an address, article, aside, blockquote, dir, div, dl, fieldset, footer, form, h1, h2, h3, h4, h5, h6, header, hr, menu, nav, ol, p, pre, section, table, or ul element, or if there is no more content in the parent element and the parent element is not an a element.
如果p元素后面紧跟一个地址,文章,aside,blockquote,dir,div,dl,fieldset,footer,form,h1,h2,h3,h4,h5,h6,标头,则可以省略p元素的结束标记,hr,menu,nav,ol,p,pre,section,table或ul元素,或者如果父元素中没有更多内容并且父元素不是a元素。
Since your HTML contains p
tag within p which act as end tag and that p
tag only contains that much text. 由于您的HTML在p内包含
p
标记,而p用作结束标记,而p
标记仅包含那么多文本。 The </p>
doesn't have any role here since p
tag is closed based on the documentation. </p>
在这里没有任何作用,因为根据文档关闭了p
标签。 Although the p
tag permitted contents are phrasing contents . 尽管
p
标签允许的内容是短语内容 。
div
instead of p
tag to make it valid and get HTML content using innerHTML
property of dom element.
div
代替p
标签来使其有效,并使用dom元素的innerHTML
属性获取HTML内容。
console.log( document.querySelector('.question').innerHTML );
<div class="question"> this is my paragraph <div>And i'm inside tag in question class <h1> And this is my heading </h1> </div> </div>
You can use: 您可以使用:
$(".question").text()
Or: 要么:
$(".question").html()
jQuery is not required. 不需要jQuery。
You need the the Element.innerHTML function. 您需要Element.innerHTML函数。 https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML
https://developer.mozilla.org/zh-CN/docs/Web/API/Element/innerHTML
I would suggest using an ID instead of a class if there is only one instance on the page. 如果页面上只有一个实例,我建议使用ID而不是类。
myString = document.getElementById('id').innerHTML;
Nesting paragraph's is bad. 嵌套段落是不好的。
The
<p>
element represents a paragraph.<p>
元素代表一个段落。 It cannot contain block-level elements (including<p>
itself).它不能包含块级元素(包括
<p>
本身)。
So 所以
console.log($('.question').html());
or 要么
console.log(document.getElementsByClassName("question")[0].innerHTML);
would fetch only " this is my paragraph " 只会获取“ 这是我的段落 ”
Proposed Solution: 建议的解决方案:
Instead wrap your content in a div
and then access the HTML. 而是将内容包装在
div
,然后访问HTML。 innerHTML
can be accessed by various methods here are few of them. innerHTML
可以通过各种方法访问,这里很少。
document.querySelector('.question').innerHTML
document.querySelector('.question').innerHTML
console.log(document.getElementsByClassName("question")[0].innerHTML);
console.log(document.getElementsByClassName("question")[0].innerHTML);
. document.getElementById(id).innerHTML
document.getElementById(id).innerHTML
Example: 例:
var t = document.getElementsByClassName("question"); console.log(t[0].innerHTML);
<div class="question"> this is my paragraph <div>And i'm inside tag in question class <h1> And this is my heading</h1> </div> </div>
Thank you all of you for giving me suggestion. 谢谢大家给我的建议。 but i solved my problem just covered question class paragraph with a div tag like that
但是我解决了我的问题,只是用div标签覆盖了问题类段落
<div class="outer">
<p class="question">
this is my paragraph<p>hello world</p> <p> And i'm inside
tag in question class <h1> And this is my heading
</h1> </p></p>
</div>
and by using $('.outer').html();
并使用
$('.outer').html();
it print all inside html. 它在html内全部打印。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.