简体   繁体   English

为什么在我使用JQuery的$ .parseHTML时缺少HTML块的外部div?

[英]Why am I missing the outer div of my HTML block when I use JQuery's $.parseHTML?

When I parse an html string with $.parseHTML() , the outer most div disappears. 当我使用$.parseHTML()解析html字符串时,最外面的div消失了。

Given the html string: 给定html字符串:

var myCode = '<div class="outer">' +
    '    <div class="inner">' +
    '        <div>' +
    '            <p>Hello!</p>' +
    '        </div>' +
    '        <div>' +
    '            <p>Hello!!' +
    '            </p>' +
    '        </div>' +
    '    </div>' +
    '</div>';

When I call parse this html, the outer most div ( class="outer" ) isn't present. 当我调用解析此html时,最外面的div( class="outer" )不存在。

var $myHtmlObject = $($.parseHTML(myCode)) 
console.log($myHtmlObject.html()) // no "outer" div present. 

I made a jsfiddle to show the behavior 我做了一个jsfiddle来显示行为

I can easily get around this by wrapping everything in another div before I parse it, but that seems like a hack for something that should otherwise work. 我可以通过在解析之前将所有内容包装在另一个 div来轻松解决此问题,但这似乎是对本应可以使用的东西的一种破解。

Because .html() gives the inner html of the first element of the set of element on which it was called. 因为.html()给出了被调用的元素集中的第一个元素的内部html。 What you are looking for here is the outerHTML 您在这里寻找的是outerHTML

console.log($myHtmlObject[0].outerHTML)
console.log($myHtmlObject.prop('outerHTML'))

Also there is no need to use parseHTML() in this case 同样在这种情况下也不需要使用parseHTML()

 var log = (function() { var $log = $('#log'); return function(msg) { $('<p/>', { text: msg }).appendTo($log) } })(); var myCode = '<div class="outer">' + ' <div class="inner">' + ' <div>' + ' <p>Hello!</p>' + ' </div>' + ' <div>' + ' <p>Hello!!' + ' </p>' + ' </div>' + ' </div>' + '</div>'; var $html = $(myCode); log('html: ' + $html.html()) log('outer-html: ' + $html.prop('outerHTML')) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="log"></div> 

暂无
暂无

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

相关问题 当我添加“显示:块”时,为什么我的 HTML 表会变成它的一半大小; 到它的 CSS? - Why does my HTML table become half it's size when I add 'display: block;' to it's CSS? 为什么在使用jQuery UI自动完成功能时会得到额外的HTML? - Why am i getting extra html when i use jquery ui autocomplete? 重构jQuery代码时,为什么会得到[object Object]而不是预期的HTML? - Why am I getting [object Object] instead of my expected HTML when refactoring jQuery code? 为什么当我在网站上上传时我的 jQuery 不起作用 - why my jQuery is not working when i am uploading on the website 我正在尝试排列我的html / css / jquery,因此我可以通过双击来切换div的可见性 - I am trying to arrange my html/css/jquery so I can toggle the visibility of a div by double clicking on it 使用Google Closure编译器处理jQuery parseHTML()和html()时, - Dealing with jQuery parseHTML() and html() when using Google closure compiler,,, 当当前页面没有加载jQuery时,为什么我能在Chrome的JS控制台中使用jQuery语法? - Why am I able to use jQuery syntax in Chrome's JS console when current page doesn't have jQuery loaded? 我正在使用 jquery 中继器 js 库来重复 html 但是当我尝试在嵌套中继器中添加元素时,外部中继器也称为 - I am using jquery repeater js library for repeat html but when i am trying to add element in nested repeater then outer repeater also called 当我使用 jQuery 的 html 时,验证码不刷新 - Captcha doesn't refresh when I use jQuery's html 我在HTML中缺少什么才能使JavaScript工作? - What am I missing in my html to make the javascript work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM