停止jquery/javascript中的空格分隔函数
Stopping space-delimited function in jquery/javascript
我有这段代码(我没有写它),它在html文档中查看<li>
类名称,然后基于它创建一个复选框。例如<li class="books"></li>
—复选框将被标记为"书籍"。然而,如果你在类中有一个空间,例如<li class="book case"></li
>,它会创建两个标签- book &的情况。我不想让它这样,我想要一个由两个单词组成的复选框。在下面的代码中,它说"现在我有一个存储的所有类名的空格分隔的字符串"。我不明白是哪一点在起作用。有人能看出有什么明显的可以改变的地方吗?
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 case
",而是把它当作"这是两个单独的类book case
"。
也许如果你改变类为book_case
,然后做一些字符串黑客替换_
与。
你真正的问题是这一行。
stringOfClassNames = stringOfClassNames +' '+ thisClassString
这将创建一个看起来像这样的字符串:
<li class="one" ></li>
<li class="two three"></li>
stringOfClassNames = 'one two three'
然后这一行被
分开stringOfClassNames.split(' ');
所以你真正需要做的是把它们塞进第一个函数的数组中。
//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
});
相关文章:
- 选择带有空格分隔单词的元素
- 匹配由单个空格分隔的单词,单词的开头或结尾不能有空格
- 如何在php和javascript中使用regex检测空格分隔词(在全文搜索查询中)
- 从空格分隔的文本创建和实现数组:AngularJS
- 在 Javascript 中将对象连接为空格分隔字符串的最简单方法是什么?
- Regex从空格分隔的字符串中删除连续重复项
- 在自定义标记中查找包含特定值的元素,并带有空格分隔的值
- 将空格分隔的类列表转换为 Jquery 选择器的有效方法
- 在输入中需要两个用空格分隔的单词
- 正则表达式替换以空格分隔的整数
- Javascript如何反转空格分隔的字符串
- Javascript/REGEX:删除字符串中以特定字母开头的特定文本(单词),单词之间用空格分隔
- 用空格分隔字符串,但不能在括号内
- 用空格分隔的字符串样式
- javascript foreach数组,并用逗号和空格分隔,显示为字符串
- 允许逗号和空格分隔的数字列表的正则表达式
- 用空格分隔的Javascript,但不能用引号分隔
- Javascript Regex匹配由空格分隔但不包含句点的字符串
- 我怎样才能在一个用逗号、换行符或空格分隔的文本区域中计算唯一的电话号码数?
- 输入模式允许多个标签,用空格分隔(#one #two)