在这段代码中我想添加弹出框链接,但在<a>.. help me

时间:2018-01-05 14:37:17

标签: php

    if (strlen($string) > 500)
        {
            $stringCut = substr($string, 0, 500);

            $string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="#" onclick=\"showAjaxModal('.base_url().'index.php?modal/popup/readmore/' . $row['categories_id'].')\"> ...Read More </a> ' ; 

        }
    echo $string;
?>

Uncaught SyntaxError: Invalid or unexpected token

3 个答案:

答案 0 :(得分:0)

试试这样:

$string = substr($string, 0, 500);
$string = substr($string, 0, strrpos($string, ' '));
$string .= '... <a href="#" onclick="showAjaxModal(\'' . base_url() . 'index.php?modal/popup/readmore/' . $row['categories_id'] . '\')">... Read More</a>';

您缺少应该在showAjaxModal函数中包装url的单引号。

答案 1 :(得分:0)

也许您忘记了网址的引号?

$string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="#" onclick=\"showAjaxModal("'.base_url().'index.php?modal/popup/readmore/' . $row['categories_id'].'")\"> ...Read More </a> ' ; 

答案 2 :(得分:0)

这是另一个更易于管理的版本:

if (strlen($string) > 500) {
  $cut = substr($string, 0, 500);
  $cut = substr($cut, 0, strrpos($cut, ' '));

  $url = base_url() . "index.php?modal/popup/readmore/" . $row['categories_id'];
  $string = "$cut... <a href='#' onclick='showAjaxModal(\"$url\")'> ...Read More </a>"; 
}
echo $string;
相关问题