admin管理员组文章数量:1026676
I have an unordered list in my HTML
<ul>
<li><div class="container1"><h2>list 1</h2></div></li>
<li><div class="container2"><h2>list 2</h2></div></li>
<li><div class="container3"><h2>list 3</h2></div></li>
</ul>
I want to get a particular <li>
html using Jquery html()
method
console.log($(".container1").closest('li').html()); // returns code from the div
output : <div class="container"><h2>list 1</h2></div>
I want the output as <li><div class="container1"><h2>list 1</h2></div></li>
including <li>
tags.
If I go one level above to closest('ul')
its returning all the 3 li elements.
JSFiddle : /
I have an unordered list in my HTML
<ul>
<li><div class="container1"><h2>list 1</h2></div></li>
<li><div class="container2"><h2>list 2</h2></div></li>
<li><div class="container3"><h2>list 3</h2></div></li>
</ul>
I want to get a particular <li>
html using Jquery html()
method
console.log($(".container1").closest('li').html()); // returns code from the div
output : <div class="container"><h2>list 1</h2></div>
I want the output as <li><div class="container1"><h2>list 1</h2></div></li>
including <li>
tags.
If I go one level above to closest('ul')
its returning all the 3 li elements.
JSFiddle : http://jsfiddle/W4QfJ/417/
Share Improve this question asked May 28, 2015 at 17:02 budhavarapu rangabudhavarapu ranga 4833 gold badges7 silver badges15 bronze badges 1-
Provided printing HTML content of that <li> in console is not the end-result here, what do you want to do with that <li> element next? If you lose the
.html()
at the end, you will have a perfectly good jQuery selector holding that same <li> element. You can do with it what you would do with any jQuery selection: remove it, move some place else, add after it, etc. – martynasma Commented May 28, 2015 at 18:03
2 Answers
Reset to default 4Try $(".container1").closest('li')[0].outerHTML
(I think it is patible for most browsers-You probably need to check for IE)
So, this is more of a documentation than the answer as @tabz100 has the asnwer.
Few ways of getting the outer HTML (in the order of my preference):
// 1)
$(".container1").closest('li').prop("outerHTML");
// 2)
$(".container1").closest('li')[0].outerHTML;
// 3)
$(".container1").closest('li').wrap("<div></div>").parent().html();
I have an unordered list in my HTML
<ul>
<li><div class="container1"><h2>list 1</h2></div></li>
<li><div class="container2"><h2>list 2</h2></div></li>
<li><div class="container3"><h2>list 3</h2></div></li>
</ul>
I want to get a particular <li>
html using Jquery html()
method
console.log($(".container1").closest('li').html()); // returns code from the div
output : <div class="container"><h2>list 1</h2></div>
I want the output as <li><div class="container1"><h2>list 1</h2></div></li>
including <li>
tags.
If I go one level above to closest('ul')
its returning all the 3 li elements.
JSFiddle : /
I have an unordered list in my HTML
<ul>
<li><div class="container1"><h2>list 1</h2></div></li>
<li><div class="container2"><h2>list 2</h2></div></li>
<li><div class="container3"><h2>list 3</h2></div></li>
</ul>
I want to get a particular <li>
html using Jquery html()
method
console.log($(".container1").closest('li').html()); // returns code from the div
output : <div class="container"><h2>list 1</h2></div>
I want the output as <li><div class="container1"><h2>list 1</h2></div></li>
including <li>
tags.
If I go one level above to closest('ul')
its returning all the 3 li elements.
JSFiddle : http://jsfiddle/W4QfJ/417/
Share Improve this question asked May 28, 2015 at 17:02 budhavarapu rangabudhavarapu ranga 4833 gold badges7 silver badges15 bronze badges 1-
Provided printing HTML content of that <li> in console is not the end-result here, what do you want to do with that <li> element next? If you lose the
.html()
at the end, you will have a perfectly good jQuery selector holding that same <li> element. You can do with it what you would do with any jQuery selection: remove it, move some place else, add after it, etc. – martynasma Commented May 28, 2015 at 18:03
2 Answers
Reset to default 4Try $(".container1").closest('li')[0].outerHTML
(I think it is patible for most browsers-You probably need to check for IE)
So, this is more of a documentation than the answer as @tabz100 has the asnwer.
Few ways of getting the outer HTML (in the order of my preference):
// 1)
$(".container1").closest('li').prop("outerHTML");
// 2)
$(".container1").closest('li')[0].outerHTML;
// 3)
$(".container1").closest('li').wrap("<div></div>").parent().html();
本文标签: javascriptJquery html() method to get ltligt htmlStack Overflow
版权声明:本文标题:javascript - Jquery html() method to get <li> html - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745650491a2161288.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论