简体   繁体   English

如何防止jQuery解析插入的HTML?

[英]How to keep jQuery from parsing inserted HTML?

Does anyone know how to stop jQuery fromparsing html you insert through before() and after()? 有谁知道如何停止jQuery frompars的html你通过before()和after()插入? Say I have an element: 说我有一个元素:

<div id='contentdiv'>bla content bla</div>

and I want to wrap it in the following way: 我想用以下方式包装它:

<div id='wrapperDiv'>
    <div id='beforeDiv'></div>
    <div id='contentDiv'>bla content bla</div>
    <div id='afterDiv'></div>
</div>

I use the following jQuery/Javascript 我使用以下jQuery / Javascript

$('#contentDiv').each( function() {
    var beforeHTML = "<div id='wrapperDiv'><div id='beforeDiv'></div>";
    var afterHTML = "<div id='afterDiv'></div></div>";  
    $(this).before(beforeHTML);
    $(this).after(afterHTML);
}

This however will not result in the correct wrapping, it will create: 然而,这不会导致正确的包装,它将创建:

<div id='wrapperDiv'>
    <div id='beforeDiv'></div>
</div>
    <div id='contentDiv'>bla content bla</div>
    <div id='afterDiv'></div>

Using wrap() won't work either since that gets jQuery even more mixed up when using: 使用wrap()也不会起作用,因为在使用时,jQuery会更加混乱:

$(this).wrap("<div id='wrapperDiv'><div id='beforeDiv'></div><div id='afterDiv'></div></div>");

How should I solve this? 我该怎么解决这个问题?
Thanks in advance! 提前致谢!

$('#contentDiv').each(function() {
    $(this).wrap('<div id="wrapperDiv">');
    $(this).before('<div id="beforeDiv">');
    $(this).after('<div id="afterDiv">');
});

produces: 生产:

<div id='wrapperDiv'>
    <div id='beforeDiv'></div>
    <div id='contentDiv'>bla content bla</div>
    <div id='afterDiv'></div>
</div>

your markup isn't complete...before and after are to take complete nodes only... 你的标记不完整...之前和之后只需要完整的节点...

what you are trying to do is wrap your content, which is different. 你要做的是包装你的内容,这是不同的。

you want this: 你要这个:

.wrap(html); .wrap(HTML);

http://docs.jquery.com/Manipulation/wrap#html http://docs.jquery.com/Manipulation/wrap#html

I think you're approaching it wrong. 我觉得你接近错了。 Think about what you actually want to achieve... 想想你真正想要实现的目标......

You want to WRAP everything with one div. 你想用一个div来包装所有东西。 Then insert 1 div before, and 1 div after. 然后在之前插入1 div,之后插入1 div。

so do .wrap() first, then append before and after-divs relative to the content-div. 所以首先执行.wrap(),然后相对于content-div追加before-after-div。

if you happen to have the actual HTML as a string (from an XHR or something) then you need to read out the html and concatenate it yourself as Douglas Mayle suggested. 如果您碰巧将实际的HTML作为字符串(来自XHR或其他东西),那么您需要读出html并自己将其连接起来,正如Douglas Mayle建议的那样。

I'm sorry, but this one should be obvious. 对不起,但这个应该是显而易见的。 In your case, you can't use wrap because it sticks the original node into the deepest node it finds in the wrapping HTML. 在您的情况下,您不能使用wrap,因为它将原始节点粘贴到它在包装HTML中找到的最深节点。 You don't want that. 你不希望这样。 Instead, read out the HTML from your object and combine it with what you have: 相反,从对象中读出HTML并将其与您拥有的相结合:

$('#contentDiv').each( function() {
    var beforeHTML = "<div id='wrapperDiv'><div id='beforeDiv'></div>";
    var afterHTML = "<div id='afterDiv'></div></div>";

    // This line below will do it...
    $(this).html(beforeHTML + $(this).html() + afterHTML);
}

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

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