查找列表项并将其 ID 转换为小写,添加连字符
Find list items and convert its ID to lowercase, add hyphens
我在同一个页面上有多个无序列表(准确地说是手风琴)。每个列表项都有一个唯一的 id,基本上是用户生成的字符(因此可以是任何东西)。我正在尝试将每个唯一 ID 转换为小写并将所有空格替换为连字符。
我正在使用的代码位于 .each
函数中,因为同一页面上有多个手风琴,它仍然输出用户生成的 ID 而不是转换后的 ID:
$('ul.responsive-accordion').each(function() {
var question = $('ul.responsive-accordion').find('li');
var questionhash = $(question).attr('id');
convertedhash = questionhash.toLowerCase().replace(/ /g, '-');
});
当我使用下面的输出到控制台时,它只输出第一个项目 ID 两次而不是其他项目(在这个例子中应该显示 3 个列表项目 ID):
// Replace spaces with hyphens
var question = $('ul.responsive-accordion').find('li');
var questionhash = $(question).attr('id');
console.log ( questionhash );
convertedhash = questionhash.toLowerCase().replace(/ /g, '-');
console.log ( convertedhash );
查看输出:
您想在循环外初始化一个变量。
var question = $('ul.responsive-accordion').find('li');
$(question).each(function() {
var questionhash = $(question).attr('id');
convertedhash = questionhash.toLowerCase().replace(/ /g, '-');
});
您没有正确使用每个功能
$('ul.responsive-accordion').each(function() {
// use this to get access to the item in the loop
var question = $(this).find('li');
var questionhash = $(question).attr('id');
convertedhash = questionhash.toLowerCase().replace(/ /g, '-');
});
或者,您也可以这样做:
$('ul.responsive-accordion').each(function(idx, elem) {
var question = $(elem).find('li');
var questionhash = $(question).attr('id');
convertedhash = questionhash.toLowerCase().replace(/ /g, '-');
});
您需要使用 each()
将它们全部循环
$('ul.responsive-accordion li').each( function() {
var id = $(this).attr('id');
console.log( id );
var newId = id.toLowerCase().replace(/ /g, '-');
console.log( newId );
$(this).attr('id', newId);
});
我在同一个页面上有多个无序列表(准确地说是手风琴)。每个列表项都有一个唯一的 id,基本上是用户生成的字符(因此可以是任何东西)。我正在尝试将每个唯一 ID 转换为小写并将所有空格替换为连字符。
我正在使用的代码位于 .each
函数中,因为同一页面上有多个手风琴,它仍然输出用户生成的 ID 而不是转换后的 ID:
$('ul.responsive-accordion').each(function() {
var question = $('ul.responsive-accordion').find('li');
var questionhash = $(question).attr('id');
convertedhash = questionhash.toLowerCase().replace(/ /g, '-');
});
当我使用下面的输出到控制台时,它只输出第一个项目 ID 两次而不是其他项目(在这个例子中应该显示 3 个列表项目 ID):
// Replace spaces with hyphens
var question = $('ul.responsive-accordion').find('li');
var questionhash = $(question).attr('id');
console.log ( questionhash );
convertedhash = questionhash.toLowerCase().replace(/ /g, '-');
console.log ( convertedhash );
查看输出:
您想在循环外初始化一个变量。
var question = $('ul.responsive-accordion').find('li');
$(question).each(function() {
var questionhash = $(question).attr('id');
convertedhash = questionhash.toLowerCase().replace(/ /g, '-');
});
您没有正确使用每个功能
$('ul.responsive-accordion').each(function() {
// use this to get access to the item in the loop
var question = $(this).find('li');
var questionhash = $(question).attr('id');
convertedhash = questionhash.toLowerCase().replace(/ /g, '-');
});
或者,您也可以这样做:
$('ul.responsive-accordion').each(function(idx, elem) {
var question = $(elem).find('li');
var questionhash = $(question).attr('id');
convertedhash = questionhash.toLowerCase().replace(/ /g, '-');
});
您需要使用 each()
$('ul.responsive-accordion li').each( function() {
var id = $(this).attr('id');
console.log( id );
var newId = id.toLowerCase().replace(/ /g, '-');
console.log( newId );
$(this).attr('id', newId);
});