簡體   English   中英

如何使用jQuery整理HTML標簽

[英]How to tidy html tag with jQuery

我如何使用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>

Notepad ++沒問題; 您的JavaScript語法有問題。 您會發現問題上突出顯示的語法也有些偏離。

該HTML片段必須位於字符串中,因為您將其用作腳本中的字符串數據:在此處輸入代碼$(“ body”)。append('');

我認為您也需要將div標簽括在引號中。

append(“ div id = \\” my_id \\“>”);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM