如何用jQuery整理html标签

时间:2013-07-10 13:34:01

标签: php jquery html

如何使用jQuery整理下面的html标签,

<p><em>bla bla bla<br /></em>someone</p>

成,

<p><em>bla bla bla</em><br />someone</p>

我想将 bla bla bla 放入容器中,然后将某人放入另一个容器中。到目前为止,我已经提出了以下代码......

            // Refresh quotes.
            var quotes = $(".quote-library");

            // Starts with 1.
            var count = 1;
            $(".button-refresh").click(function(){

                var current = $("p:eq("+ count +")",quotes).text();
                alert(current);
                //console.log(count + 1);

                // Break the string into an array with the line break.
                var array = current.split("\n");
                //alert(array[0]);
                //console.log(count + 1);

                // Put the text into the containers.
                $(".quote-italic").empty().html("<em>"+ array[0] +"</em>");
                $(".quote-normal").empty().html(array[1]);

                // Increase the counter on each click.
                count ++;

                // Increase the counter on each click.
                count ++;

                // Reset count counter when it reaches the limit.
                if(count == $("p",quotes).length) count = 0;
                return false;
            });

HTML,

<div class="quote-library" style="display:none;">
    <p><em>Man is a creation of desire, not a creation of need.</em><br /> Gaston Bachelard</p>
    <p><em>We are like butterflies who flutter for a day and think it's forever.</em><br /> Carl Sagan</p>
    <p><em>Imagination will often carry us to worlds that never were. But without it we go nowhere.</em><br /> Carl Sagan</p>
    <p><span><em>One must always maintain one's connection to the past and yet ceaselessly pull away from it.</em><br /> Jean-Paul Sartre<br /></span></p>
    <p><span><em>Man is condemned to be free; because once thrown into the world, he is responsible for everything he does.</em><br /> Jean-Paul Sartre<br /></span></p>
    <p><span><em>Hell is other people.</em><br /> Jean-Paul Sartre<br /></span></p>
    <p><em>bla bla bla<br /></em>someone</p>
</div>

<div class="holder-quote">
    <p class="quote-voltaire">
        <span class="quote-italic"><i>A bottle of wine contains more philosophy than all the books in the world.</i></span>
        <span class="quote-normal">Louis Pasteur</span>
    </p>
    <a href="#" class="button-refresh hide-text">refresh</a>
</div>

1 个答案:

答案 0 :(得分:0)

这不是Notepad ++的问题;这是您的JavaScript语法的问题。您会看到在您的问题上突出显示的语法也有点偏差。

HTML片段需要在字符串中,因为你在脚本中使用它作为字符串数据:在这里输入代码$(“body”)。append('');

我认为你还需要将div标签括在引号中。

追加(“div id = \”my_id \“&gt;”);