Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery每个函数在另一个jQuery每个函数中拆分出要在第一个函数中使用的数据_Jquery_Variables_Loops_Split_Each - Fatal编程技术网

jQuery每个函数在另一个jQuery每个函数中拆分出要在第一个函数中使用的数据

jQuery每个函数在另一个jQuery每个函数中拆分出要在第一个函数中使用的数据,jquery,variables,loops,split,each,Jquery,Variables,Loops,Split,Each,好的,我很抱歉第一次没有发布完整的问题,我被社区痛打了一顿,所以这是我的第二次尝试: 我在我的页面上的每篇文章中都有一个UL的LIs,我想使用JavaScript(jQuery)获取我在LIs中查找的文本,并将该文本作为类放入每篇文章中,这样我就可以用jQuery对它们进行过滤。问题是,我在每个LI中查找的值需要拆分,目前为止,我尝试从整个UL中拆分值时失败,我需要将空格转换为破折号等,因此我认为我需要为LIs运行另一个each函数,到目前为止,所有尝试都失败了 我的页面上一篇文章的HTML示例

好的,我很抱歉第一次没有发布完整的问题,我被社区痛打了一顿,所以这是我的第二次尝试:

我在我的页面上的每篇文章中都有一个UL的LIs,我想使用JavaScript(jQuery)获取我在LIs中查找的文本,并将该文本作为类放入每篇文章中,这样我就可以用jQuery对它们进行过滤。问题是,我在每个LI中查找的值需要拆分,目前为止,我尝试从整个UL中拆分值时失败,我需要将空格转换为破折号等,因此我认为我需要为LIs运行另一个each函数,到目前为止,所有尝试都失败了

我的页面上一篇文章的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>

  • 种类:胡桃
  • 产品类型:板坯
  • 标签号:xxxx
  • 请注意:xxxx
  • BF:xx.xx
  • 重量(磅):xxx.xx
  • 宽度(英寸):25-42
  • 长度(英寸):73-96
  • 深度(英寸):5
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)

    });

}
函数prepareIso(){
//在每个上循环
$('article')。每个(函数(){
//这篇文章需要的数据列表
var data=$(this.find('.ct自定义字段块ul');
//一些惊人的正则表达式替换或拆分可以返回我需要的每个值,不幸的是,我没有比这个替换链更好的解决方案,它不能提供足够的控制来创建带有连字符的类并删除不相关的数据
//var dataHTML=data.html();
//var outPut=dataHTML.replace(“种类:”,“”)。replace(“产品类型:”,“”)。replace(“标签号:”,“”)。replace(“PN:”,“”)。replace(“BF:”,“”)。replace(“重量(磅):”,“”)。replace(“宽度(英寸):”,“”)。replace(“长度(英寸):”,“”)。replace(“深度(英寸):”,“”)
//所以我想我需要另一个循环,这次只是为了LIs
$(数据)。查找('li')。每个(函数(){
$(this.html().split(“”)
//??我被困在这里,我可以拆分LI的数据,但我不知道如何获取第二个值并将其收集到字符串中。
var outPut=$(this)[1]/???尝试收集每个LI拆分后的第二个值
//我还需要在这个循环中运行regex replace,用连字符替换空格。
//但是,我需要每个“类”之间有一个空格
//剥离HTML元素应该不会有问题
//理论上,此时的输出应该是这样的
//var输出='Walnut Claro板xxxx 25-42 73-96 5'
});
//现在很简单,只需强制数据小写,并将其作为类添加到文章中即可。
var classesToAdd=outPut.toLowerCase();
$(this).addClass(classesToAdd)
});
}
因此,我基本上已经在LIs的每个函数的第二个时刻遇到了一堵墙,我知道如何分割数据并获取第二个值(如果只有一个LI),但是我如何在所有LIs上作为循环运行,并只保留分割的[1]值


我希望这能充分解释我的问题。我只是不知道这叫什么,做这样的事,我很想知道!谢谢大家!

使用RegExp来隔离所需的子字符串,而不是进行大量拆分:

function prepareIso() {
  // loop on each <article>
  $('article').each(function() {
      var classList = [];

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

      $(data).find('li').each(function() {
          var text = this.innerText;
          text = text.replace(/^.*:\s/, '');
          console.log(text);
          classList.push(text.toLowerCase().replace(' ', '-').replace('.', '_'));
      });

      $(this).addClass(classList.join(' '));
  });
};
函数prepareIso(){

//在每个

上循环,而不是进行大量拆分,使用RegExp隔离所需的子字符串:

function prepareIso() {
  // loop on each <article>
  $('article').each(function() {
      var classList = [];

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

      $(data).find('li').each(function() {
          var text = this.innerText;
          text = text.replace(/^.*:\s/, '');
          console.log(text);
          classList.push(text.toLowerCase().replace(' ', '-').replace('.', '_'));
      });

      $(this).addClass(classList.join(' '));
  });
};
函数prepareIso(){

//每个循环

好吧!我想我的答案是复习我的正则表达式!有人投票给我,因为我不能!谢谢!伟大的建议!当然。如果它最终对你有用,不要忘记选择它作为答案。干杯。所以我实际上在这一过程中遇到了更多的挑战,一个是。innerText不受Fi支持refox和另一个,当jQuery使用.text()(跨浏览器)返回变量时,它在我需要的信息上方有一个新行。因此我重写了.replace reg表达式以删除多余的行,然后执行完全相同的操作。Tada!现在在Firefox.text.replace(/\r?\n|\r/g')。replace(/^.*.\s/,'');不错。我不知道FF不喜欢
innerText
。很高兴知道。是的,FF使用.textContent,这是IE使用的冗长辩论之一。innerText和Opera都使用,其中FF是唯一一个不懂的。innerText,但它有这样做的正当理由,等等。好吧!我想我这里的答案是对m的复习y Regex!有人投票,因为我不能!谢谢!很好的建议!当然。如果它最终对你有效,不要忘记选择它作为答案。干杯。所以我实际上遇到了更多的挑战,一个是.innerText不受Firefox支持,另一个是当jQuery使用.text()返回变量时(跨浏览器)它在我需要的信息上方有一个新行。因此,我重写了.replace reg表达式以删除多余的行,然后执行其他完全相同的操作。Tada!现在在Firefox.text.replace(/\r?\n |\r/g')。replace(/^.*:\s/,'');不错。不知道FF不喜欢
innerText
。很高兴知道。是的,FF使用.textContent,这是IE使用的冗长辩论之一。innerText和Opera都使用,其中FF是唯一一个不理解.innerText的人,但它有充分的理由这样做