简体   繁体   English

为什么 jQuery 没有将此 html 字符串转换为 jQuery 对象?

[英]Why is jQuery not turning this html string into a jQuery object?

I have a template string that I need to turn into a jQuery object so I can parse it and fill out the data.我有一个模板字符串,我需要将其转换为 jQuery 对象,以便我可以解析它并填写数据。

$(function(){

var template = '<h3>Details</h3>' +
    '<ul>' + 
    '<li>Submitted: <span class="submittedTime"></span></li>' + 
    '<li>Submitted by: <span class="submitter"></span></li>' + 
    '<li>Contact Information' +
        '<ul>' +
        '<li>Name: <span class="contactName"></span></li>' +
        '<li>Name: <span class="contactEmail"></span></li>' +
        '</ul>' +
    '</li>';


var t = $(template);

});

When I try to perform a jQuery operation on them (such as .find() ), it fails.当我尝试对它们执行 jQuery 操作(例如.find() )时,它失败了。

Do you have any ideas on what I'm doing wrong?你对我做错了什么有什么想法吗?

I think it actually is, as per Quinten's comment .我认为它实际上是,根据Quinten 的评论 Try running this script against an HTML document with a <div class="test"> element:尝试针对带有<div class="test">元素的 HTML 文档运行此脚本:

$(function () {

    var template =
        "<h3>Details</h3>" +
        "<ul>" +
        "<li>Submitted: <span class='submittedTime'></span></li>" +
        "<li>Submitted by: <span class='submitter'></span></li>" +
        "<li>Contact Information" +
        "<ul>" +
        "<li>Name: <span class='contactName'></span></li>" +
        "<li>Name: <span class='contactEmail'></span></li>" +
        "</ul>" +
        "</li>" +
        "</ul>";


    var t = $(template);
    //console.log(t);
    $(t).appendTo(".test");
});

See http://jsfiddle.net/5svLfq4r/ for a demo.有关演示,请参阅http://jsfiddle.net/5svLfq4r/

You need to wrap your snippet inside a div element and close your outer ul tag.您需要将代码片段包装在 div 元素中并关闭外部ul标签。 If you run the snippet below, jQuery will correctly output the HTML.如果您运行下面的代码段,jQuery 将正确输出 HTML。 I have converted your template into a multi-line statement to make it easier to read我已将您的模板转换为多行语句,以便于阅读

 a = $(function() { var template = ` <div> <h3>Details</h3> <ul> <li>Submitted: <span class="submittedTime"></span></li> <li>Submitted by: <span class="submitter"></span></li> <li> Contact Information <ul> <li>Name: <span class="contactName"></span></li> <li>Name: <span class="contactEmail"></span></li> </ul> </li> </ul> </div>`; var t = $(template); document.write(t.html()) }); a()
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Based on the documentation :基于文档

To ensure cross-platform compatibility, the snippet must be well-formed.为确保跨平台兼容性,代码段必须格式正确。 Tags that can contain other elements should be paired with a closing tag:可以包含其他元素的标签应该与结束标签配对:

So you probably need to close that <ul> .所以你可能需要关闭那个<ul>

Also,还,

If the HTML is more complex than a single tag without attributes, [..] the actual creation of the elements is handled by the browser's .innerHTML mechanism.如果 HTML 比没有属性的单个标签更复杂,[..] 元素的实际创建由浏览器的 .innerHTML 机制处理。 In most cases, jQuery creates a new element and sets the innerHTML property of the element to the HTML snippet that was passed in.大多数情况下,jQuery 会创建一个新元素并将该元素的 innerHTML 属性设置为传入的 HTML 片段。

it may be different on different browsers.在不同的浏览器上可能会有所不同。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM