2011-09-01 5 views
0

HTML 문서에서 <li> 클래스 이름을 확인한 다음이 확인란을 기반으로이 코드를 작성했습니다. 예 : <li class="books"></li> - 확인란에는 'books'라는 라벨이 지정됩니다. 그러나 클래스에 공간이있는 경우 <li class="book case"></li> 두 개의 태그, 즉 &이 생성됩니다. 나는 이것을하기를 원하지 않는다. 나는 두 단어로 구성된 체크 박스를 가질 수 있기를 원한다. 아래 코드에서 "이제는 모든 클래스 이름의 공백으로 구분 된 문자열이 저장되었습니다."라고 말합니다. 나는 어느 비트가 이것을하고 있는지 이해하지 못한다. 이 일이 일어나지 않도록 변경 될 수있는 명백한 것을 누군가는 볼 수 있습니까? 이 필요한 경우 더 많은 코드가jquery/javascript에서 공백으로 구분 된 함수 중지

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; 

...

자바 스크립트는 두 CSS 클래스 bookcase는 하나의 book case 단어로 취급되어야한다는 것을 알고 가정되는 방법
+0

'stringOfClassNames의 =의 stringOfClassNames + ''+ thisClassString'하면 문자열을하고있다. – Jack

답변

0

? 공백 문자는 CSS 클래스 이름의 유효한 구성 요소가 아니므로 "This is a single css class book case"이 될 수는 없지만 "이 두 클래스는 별도의 클래스 book case 다른 위치"로 취급해야합니다.

아마도 클래스를 book_case으로 변경 한 다음 _으로 바꾸는 문자열 해킹을했을 수 있습니다.

0

실제 문제는이 줄입니다.

그런 다음이 줄

stringOfClassNames.split(' '); 

으로 분할됩니다

<li class="one" ></li> 
<li class="two three"></li> 
stringOfClassNames = 'one two three' 

그래서 당신이 정말로해야 할 일을 그냥 그들을 물건 :

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 
}); 
0
// 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 
}); 
관련 문제