1

わかりました、最初に完全な質問を投稿しなかったことをお詫びします。コミュニティにかなりひどい打撃を受けたので、2回目の試みです:

ページの各記事に LI の UL があります。JavaScript (jQuery) を使用して、LI で探しているテキストを取得し、そのテキストを各記事にクラスとしてドロップして、jQuery Isotope でフィルタリングできるようにします。 . 問題は、各 LI で探している値を分割する必要があることです。これまでのところ、UL 全体から値を分割しようとすると失敗しました。スペースをダッシュ​​などに変換する必要があるため、 LI のためだけに別の関数を実行する必要があると考えていますが、これまでのところ、すべての試みはここでも失敗しています。

私のページの 1 つの記事の HTML の例:

<article id="post-136" class="isotope-item">
    <div class="entry-content">
        <div class="ct-custom-field-block">
            <ul>
            <li>
                <span>Species:</span> Walnut Claro</li>
            <li>
                <span>Product Type:</span> Slab</li>
            <li>
                <span>Tag Number:</span> xxxx</li>
            <li>
                <span>PN:</span> xxxx</li>
            <li>
                <span>BF:</span> xx.xx</li>
            <li>
                <span>Weight (lbs):</span> xxx.xx</li>
            <li>
                <span>Width (in):</span> 25-42</li>
            <li>
                <span>Length (in):</span> 73-96</li>
            <li>
                <span>Depth (in):</span> 5</li>
            </ul>
        </div>
    </div> <!-- .entry-content -->
</article>

jQuery: 各記事のループ内で各 LI を個別に分割する必要があります。これまでのところ、精神的な壁にぶつかっています。この種の質問を探す場所がわかりません。ここに概念があることはわかっています。まだ理解していないので、説明やヒントをいただければ幸いです。

function prepareIso() {

    // loop on each <article>
    $('article').each(function() {

      // the list of data I need for this article
      var data = $(this).find('.ct-custom-field-block ul');

      // some amazing regex replace or split that can return each individual value I need, unfortunately I don't have a better solution than this replace chain, which doesn't give enough control to create classes with hyphens and remove irrelevant data
      // var dataHTML = data.html();
      // var outPut = dataHTML.replace("Species: ","").replace("Product Type:","").replace("Tag Number:","").replace("PN:","").replace("BF:","").replace("Weight (lbs):","").replace("Width (in):","").replace("Length (in):","").replace("Depth (in):","")

      // So I think I need another loop here, just for the LIs this time
        $(data).find('li').each(function() {
            $(this).html().split('</span> ')
            // ??? I am stuck here, I can split the LI's data but I don't know how to grab the second value and collect them into a string.
            var outPut = $(this)[1] // ??? trying to collect the second value after the split of each LI
            // I also need to run a regex replace in this loop to replace spaces with hyphens.
            // But, I need each "Class" to be separated by a space
            // Shouldn't have a problem stripping out the HTML elements
            // outPut should theoretically look something like this at this point
            // var outPut = 'Walnut-Claro Slab xxxx 25-42 73-96 5'
        });

        // now it's easy, just force the data to lower case and add as classes to the Article.
      var classesToAdd = outPut.toLowerCase();
      $(this).addClass(classesToAdd)

    });

}

したがって、私は基本的に、LI の各関数で 2 番目に壁にぶつかりました。データを分割し、LI が 1 つだけの場合は 2 番目の値を取得する方法を知っていますが、それをすべての LI でループとして実行し、値のみを保持するにはどうすればよいですか?分割 [1] 値。

これが私の問題の十分な説明であることを願っています。これが何と呼ばれているのかわかりませんが、このようなことをしているのですが、ぜひ知りたいです! ありがとうございました!

4

1 に答える 1