1

私は「Pillow BALANCE Purple」という商品タイトルを持っていて、2 番目の単語の後に改行を入れたいと思っています。以下のように。

ピローバランス
パープル

このタイトルには、「タイトル」と言うクラスがあります。次のコードを使用して最初の単語「Pillow」の色を変更しますが、2 番目の単語の色も変更し、もちろん 3 番目の単語を 2 行目に移動する必要があります。どんな助けでも感謝します。

//add span to first word of module title, page heading
window.addEvent ('domready', function () {
    var els = $$('.art-postcontent h2 a, .art-postcontent h2 a:link, .art-postcontent h2 a:hover, .art-postcontent h2 a:visited, .art-blockcontent h2 a, .art-blockcontent h2 a:link, .art-blockcontent h2 a:hover, .art-blockcontent h2 a:visited');
    if (!els.length) return;
    els.each (function(el){
         var html = el.innerHTML;
         var text = el.get("text");
         var pos = text.indexOf(' ');
         if (pos!=-1) {
            text = text.substr(0,pos);
         }
         el.set("html", el.get("text").replace(new RegExp (text), '<span class="first-word">'+text+'</span>'));

    });
});
4

1 に答える 1

0

ここでは、split() 関数を使用すると役立ちます。

var str = 'pillow BALANCE purple';
var substr = str.split(' ');    //splits the string at each space (&nbsp;)

$('#firstLine').text(substr[0] + substr[1]);  //adds first two words to first line
$('#secondLine').text(substr[2]);  //add third word to second line

HTML:

<div id="firstLine"></div>
<br />     <!-- Break will put the next div on a new line -->
<div id="secondLine"></div>
于 2013-04-09T15:19:01.953 に答える