donglang9880 2018-06-19 19:33
浏览 81

PHP html_decode / html_entity_decode打破了JS的页面

I'm having a little problems trying to print HTML in one page. I have a substr to show the first X chars of the text, and a button to show more. My problem is that if I use html_decode or html_entity_decode to print as HTML (and not only as html), it breaks the page and show all the text instead of the first X chars and the "Show more" button.

PHP:

    $item->description =  html_decode($item->description);
if( strlen( $item->description ) > 70 ) {
    $item->description = substr($item->description, 0, 50 ) .
    '<span class="more-link">... <a href="#"><i class="fa fa-caret-down"></i> ' . t( 'theme_more', 'More' ) . '</a></span>' .
    '<span class="hidden-part">' . substr( $item->description, 50 )  . '</span>' .
    '<span class="less-link"><a href="#"><i class="fa fa-caret-up"></i> ' . t( 'theme_less', 'Less' ) . '</a></span>';
}

The JS Code:

$( '.description .more-link a, .description .less-link a' ).on( 'click', function(e) {
e.preventDefault();
var desc = $(this).parents( '.description' );
var part = desc.find( '.hidden-part' ),
more = desc.find( '.more-link' ),
less = desc.find( '.less-link' );
if( part.is( ':visible' ) ) {
    part.hide();
    more.show();
    less.hide();
} else {
    part.show();
    more.hide();
    less.show();
}

});

If I change the html_decode to strip_tags just to debug, it works as supposed, but prints the HTML chars as plain text (show the first X chars and then in the Show More button, it shows the rest of the text). With html decode, basically it shows all the text and create the button in the middle of the text.

My problem is not about the HTML tags being cutted with substr.

Can anyone help me please? Thanks

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数