首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想添加活动类只在第一个孩子元素php它是一个基于放大器的代码

我想添加活动类只在第一个孩子元素php它是一个基于放大器的代码
EN

Stack Overflow用户
提问于 2021-04-20 01:36:33
回答 1查看 32关注 0票数 0

我只想在li的第一个元素中添加活动类。

这是一个放大器代码,...jquery不能正常工作,我将如何实现这种逻辑请帮助

代码语言:javascript
复制
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;
    }
EN

回答 1

Stack Overflow用户

发布于 2021-04-20 01:44:26

如果要为第一个标记添加活动类,请替换下面的行

代码语言:javascript
复制
$activeClass = ($checkvalue<$tag)?"active":"";
$tableOfContents .= "<li class='$id $activeClass'><a href='#$id'>$title</a></li>";

完整代码

代码语言:javascript
复制
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;

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67166740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档