0

我必须使用jQuery .html()方法在 DIV 中动态加载以下 HTML。

<div class="box-gerenciamento-versoes-variacoes">
    <div class="detalhes-versao">
        <p><span>Cor</span> do produto:</p> <img src="/admin/imagens/cores/preto-amarelo.png" height="18" width="18" title="Preto e amarelo" alt="" /> <span class="nome-cor">Preto e amarelo</span>
    </div>

    <div class="box-enviar-fotos">
        <input type="button" class="botao-enviar-fotos" value="" /> 0 fotos enviadas
    </div>

    <div class="div_clear"></div>

    <div class="etiquetas-tamanho-estoque">
        <p class="etiqueta-tamanho">Tamanho:</p>
        <p class="etiqueta-estoque">Estoque:</p>
    </div>

    <ul class="gerenciamento-tamanho">
        <li>
            PP
            <input type="text" />
        </li>
        <li>
            P
            <input type="text" />
        </li>
        <li>
            M
            <input type="text" />
        </li>
        <li>
            G
            <input type="text" />
        </li>
    </ul>

    <div class="div_clear"></div>
</div>

如您所见,内容不小,并且该.html()方法不允许换行,因此要加载此内容我必须删除所有换行符,但是这样做代码会很丑,应用程序维护会很困难.

.html()如何在保持良好可读性的同时使用 jQuery 方法加载大部分 HTML 内容?

4

2 回答 2

12
alert("Just put a backslash\
at the end of your lines\
to allow line breaks\
in JavaScript");
于 2012-04-19T15:35:59.453 回答
2
var newHtml = $('.box-gerenciamento-versoes-variacoes').html().replace(/(\r\n|\n|\r)/gm,"");
$('.box-gerenciamento-versoes-variacoes').empty().html(newHtml);
于 2012-04-19T15:48:31.193 回答