我只想在li的第一个元素中添加活动类。
这是一个放大器代码,...jquery不能正常工作,我将如何实现这种逻辑请帮助
function custom_table_of_contents($content) {
global $tableOfContents;
global $checkvalue;
$tableOfContents = '';
$tableOfContents = "<ul class='toc-item-list'>";
$checkvalue = 0;
$index = 1;
$indexes = [2 => 1, 3 => 0, 4 => 0, 5 => 0, 6 => 0];
// Insert the IDs and create the TOC.
$content = preg_replace_callback('#<(h[1-6])(.*?)>(.*?)</\1>#si', function ($matches) use (&$index, &$tableOfContents, &$indexes, &$checkvalue) {
$tag = $matches[1];
$title = strip_tags($matches[3]);
$hasId = preg_match('/id=(["\'])(.*?)\1[\s>]/si', $matches[2], $matchedIds);
$id = $hasId ? $matchedIds[2] : sanitize_title($title);
// Generate the prefix based on the heading value.
//$prefix = '';
foreach (range(2, $tag[1]) as $i) {
if ($i == $tag[1]) {
$indexes[$i] += 1;
}
//$prefix .= $indexes[$i] . '.';
}
$title = "$title";
//Check the tag value
if($checkvalue<$tag)
{
//Condition where previous tag value is lesser than the current one
$tableOfContents .= '<ul>';
} else if ($checkvalue==$tag) {
//Condition where previous tag value is same as that of current one
} else if($checkvalue>$tag){
$tableOfContents .='</ul>';
}
$checkvalue=$tag;
$tableOfContents .= "<li class='$id active'><a href='#$id'>$title</a></li>";
if ($hasId) {
return $matches[0];
}
return sprintf('<%s%s id="%s">%s</%s>', $tag, $matches[2], $id, $matches[3], $tag);
}, $content);
$tableOfContents .= '</ul>';
echo $tableOfContents;
return $content;
}发布于 2021-04-20 01:44:26
如果要为第一个标记添加活动类,请替换下面的行
$activeClass = ($checkvalue<$tag)?"active":"";
$tableOfContents .= "<li class='$id $activeClass'><a href='#$id'>$title</a></li>";完整代码
function custom_table_of_contents($content) {
global $tableOfContents;
global $checkvalue;
$tableOfContents = '';
$tableOfContents = "<ul class='toc-item-list'>";
$checkvalue = 0;
$index = 1;
$indexes = [2 => 1, 3 => 0, 4 => 0, 5 => 0, 6 => 0];
// Insert the IDs and create the TOC.
$content = preg_replace_callback('#<(h[1-6])(.*?)>(.*?)</\1>#si', function ($matches) use (&$index, &$tableOfContents, &$indexes, &$checkvalue) {
$tag = $matches[1];
$title = strip_tags($matches[3]);
$hasId = preg_match('/id=(["\'])(.*?)\1[\s>]/si', $matches[2], $matchedIds);
$id = $hasId ? $matchedIds[2] : sanitize_title($title);
// Generate the prefix based on the heading value.
//$prefix = '';
foreach (range(2, $tag[1]) as $i) {
if ($i == $tag[1]) {
$indexes[$i] += 1;
}
//$prefix .= $indexes[$i] . '.';
}
$title = "$title";
//Check the tag value
if($checkvalue<$tag)
{
//Condition where previous tag value is lesser than the current one
$tableOfContents .= '<ul>';
}
else if ($checkvalue==$tag)
{
//Condition where previous tag value is same as that of current one
}
else if($checkvalue>$tag){
$tableOfContents .='</ul>';
}
$checkvalue=$tag;
$activeClass = ($checkvalue<$tag)?"active":"";
$tableOfContents .= "<li class='$id $activeClass'><a href='#$id'>$title</a></li>";
if ($hasId) {
return $matches[0];
}
return sprintf('<%s%s id="%s">%s</%s>', $tag, $matches[2], $id, $matches[3], $tag);
}, $content);
$tableOfContents .= '</ul>';
echo $tableOfContents;
return $content;}
https://stackoverflow.com/questions/67166740
复制相似问题