jquery/javascript中的停止空格分隔函数

jquery/javascript中的停止空格分隔函数,javascript,jquery,Javascript,Jquery,我有一段代码(不是我写的),它查看html文档中的类名称,然后根据它创建一个复选框。e、 g-复选框将标记为“books”。但是,如果类中有空格,例如则会创建两个标记-book和case。我不希望它这样做,我希望能够有一个由两个单词组成的复选框。在下面的代码中,它说“现在我有一个由所有类名组成的空格分隔的字符串”。但我不明白是哪一位在这么做。有人能看到任何明显的可以改变的东西,这样就不会发生这种情况吗 var stringOfClassNames = ''; // grab the class

我有一段代码(不是我写的),它查看html文档中的类名称,然后根据它创建一个复选框。e、 g
  • -复选框将标记为“books”。但是,如果类中有空格,例如
  • 则会创建两个标记-book和case。我不希望它这样做,我希望能够有一个由两个单词组成的复选框。在下面的代码中,它说“现在我有一个由所有类名组成的空格分隔的字符串”。但我不明白是哪一位在这么做。有人能看到任何明显的可以改变的东西,这样就不会发生这种情况吗

    var stringOfClassNames = '';
    
    // grab the class name of each list item to build that string
    $('.filterThis > li').each( function (i) {
        var thisClassString = $(this).attr('class');
        stringOfClassNames = stringOfClassNames +' '+ thisClassString
    });
    
    // now i have a space-delimited string of all class names stored
    // in the stringOfClassNames variable.  
    // Trim spaces from the ends of that string:
    stringOfClassNames = jQuery.trim(stringOfClassNames);
    
    // i can't really do anything with it until it's an array, so
    // convert that string to an array.
    var arrayClasses = stringOfClassNames.split(' ');
    
    
    // now for the isolating the filter that is common to all.
    // must do before extracting only the unique classes
    // one way to approach: count the number of times classes occur, and
    // if any occur the same number of times as how many list items i have,
    // assume that class is common to all list items, and remove it from
    // the filter list. duplicate class on same item = problem, but 
    // i'm not thinking about that right now.
    // i've also chosen sort the pre-unique'd array
    // instead of sorting the unique'd array.  i think i have to for the count.
    var arrayClasses = arrayClasses;
    totalNumberOfItemsToFilter = $('.filterThis > li').length;
    

    如果需要,还有更多的代码…

    Javascript如何知道两个css类
    book
    case
    应该被视为一个
    book case
    单词?空格字符不是CSS类名的有效组成部分,因此不能将其设置为“这是一个单独的CSS类<代码>书壳”,而应将其视为“这是两个单独的类<代码>书壳


    也许如果您将类更改为
    book\u case
    ,然后进行一些字符串黑客操作,将
    替换为

    ,您真正的问题是这一行

    stringOfClassNames = stringOfClassNames +' '+ thisClassString
    
    这将创建一个如下所示的字符串:

    <li class="one" ></li>
    <li class="two three"></li>
    stringOfClassNames = 'one two three'
    
    因此,您真正需要做的是将它们填充到第一个函数的数组中

     //pseudocode since I think you should learn to program this yourself. Pretty basic.
     var arrayClasses
     $('.filterThis > li').each( function (i) {
      //get all the class names for this li
      var thisClassString = $(this).attr('class');
      //stick them into an array
      var splitClasses = thisClassString.split(' ');
      //now add splitClasses to arrayClasses
    });
    

    stringOfClassNames=stringOfClassNames+“”+thisClassString
    正在生成字符串。
     //pseudocode since I think you should learn to program this yourself. Pretty basic.
     var arrayClasses
     $('.filterThis > li').each( function (i) {
      //get all the class names for this li
      var thisClassString = $(this).attr('class');
      //stick them into an array
      var splitClasses = thisClassString.split(' ');
      //now add splitClasses to arrayClasses
    });
    
    // grab the class name of each list item to build that string
    $('.filterThis > li').each( function (i) {
        var thisClassString = $(this).attr('class');
    
        // replace all spaces in this element's classes 
        // in order to turn multiple classes into one class
        thisClassString.replace(/\s/g, '');
    
        stringOfClassNames = stringOfClassNames +' '+ thisClassString
    });