亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 span 標簽 HTML DOM 替換類

從 span 標簽 HTML DOM 替換類

PHP
白板的微信 2023-04-28 15:24:25
我正在嘗試在 HTML 類中替換和添加一些值...我在某種程度上成功地解決了這個問題,但現在我需要幫助。這是我的功能代碼function wpse247219_custom_pagination() {    global $the_query;    $big = 999999999; // need an unlikely integer    $pages = paginate_links( array(        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),        'format' => '?paged=%#%',        'current' => max( 1, get_query_var('page') ),        'total' => $the_query->max_num_pages,        'prev_next' => false,        'type'  => 'array',        'prev_next'   => true,        'prev_text'    => __( 'Previous', 'text-domain' ),        'next_text'    => __( 'Next page', 'text-domain'),    ) );    $output = '';    if ( is_array( $pages ) ) {        $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var( 'paged' );        $output .=  '<ul class="pagination-list">';        foreach ( $pages as $page ) {            $output .= "<li>$page</li>";        }        $output .= '</ul>';        $dom = new \DOMDocument();        $dom->loadHTML( mb_convert_encoding( $output, 'HTML-ENTITIES', 'UTF-8' ) );        $xpath = new \DOMXpath( $dom );        $page_numbers = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' page-numbers ')]" );        foreach ( $page_numbers as $page_numbers_item ) {            $page_numbers_item_classes = explode( ' ', $page_numbers_item->attributes->item(0)->value );            if ( in_array( 'current', $page_numbers_item_classes ) ) {                          $list_item_attr_class = $dom->createAttribute( 'class' );                $list_item_attr_class->value = 'mynewclass';                $page_numbers_item->parentNode->appendChild( $list_item_attr_class );            }現在我還想替換<span class="page-numbers current">to的類<span class="pagination-link is-current "> ,使其看起來像這樣我還試圖<li></li>從下一頁和上一頁按鈕中刪除標簽。任何類型的幫助將不勝感激
查看完整描述

2 回答

?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

在對我的代碼進行一些實驗后,我解決了我的問題。這是我的代碼...


function wpse247219_custom_pagination() {

    global $the_query;

    $big = 999999999; // need an unlikely integer

    $pages = paginate_links( array(

        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),

        'format' => '?paged=%#%',

        'current' => max( 1, get_query_var('page') ),

        'total' => $the_query->max_num_pages,

        'prev_next' => false,

        'type'  => 'array',

        'prev_next'   => true,

        'prev_text'    => __( 'Previous', 'text-domain' ),

        'next_text'    => __( 'Next page', 'text-domain'),

    ) );

    $output = '';


    if ( is_array( $pages ) ) {

        $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var( 'paged' );

        $size = count($pages)-1;

        $output .=  '<ul class="pagination-list">';

        $i = 0;

        foreach ( $pages as $page ) {

            if($i < 1 || $i == $size){

                $output .= "$page";

            }else{

                $output .= "<li>$page</li>";

            }

              $i++;  

        }

        $i = 0;

        $output .= '</ul>';

        $dom = new \DOMDocument();

        $dom->loadHTML( mb_convert_encoding( $output, 'HTML-ENTITIES', 'UTF-8' ) );

        $xpath = new \DOMXpath( $dom );

        $page_numbers = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' page-numbers ')]" );

        foreach ( $page_numbers as $page_numbers_item ) {

            $page_numbers_item_classes = explode( ' ', $page_numbers_item->attributes->item(0)->value );

            if ( in_array( 'current', $page_numbers_item_classes ) ) {          

                $list_item_attr_class = $dom->createAttribute( 'class' );

                $list_item_attr_class->value = 'mynewclass';

                $page_numbers_item->parentNode->appendChild( $list_item_attr_class );

            }


            // Replace the class 'page-numbers' with 'page-link'

            $page_numbers_item->attributes->item(0)->value = str_replace( 

                            'page-numbers',

                            'pagination-link',

                            $page_numbers_item->attributes->item(0)->value );


                             // Replace the class 'page-numbers' with 'page-link'

            $page_numbers_item->attributes->item(0)->value = str_replace( 

                            'prev pagination-link',

                            'pagination-previous',

                            $page_numbers_item->attributes->item(0)->value );

        }

        $yupo = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' page-numbers current')]" );

        foreach($yupo as $yup){

            $yup->attributes->item(1)->value = str_replace( 

                            'page-numbers current',

                            'pagination-link is-current',

                            $yup->attributes->item(1)->value );

        }



        // Save the updated HTML and output it.

        $output = $dom->saveHTML();

    }


    return $output;

}


查看完整回答
反對 回復 2023-04-28
?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

您可以僅使用 vanilla JavaScript 輕松實現此目的:


const elActivePage = document.querySelector('span.current');

elActivePage.className = 'pagination-link is-current';


查看完整回答
反對 回復 2023-04-28
  • 2 回答
  • 0 關注
  • 182 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號