这是我用来通过PHP粘贴HTML的一长行代码:

echo "<h2 style=\"color: white!important; background: black!important; border: 4px black solid!important;\">", $p_name, "<span id=\"portfolio-", $p_id, "\" onclick=\"expandToggle(this);\" style=\"float: right; padding-right: 10px; cursor: pointer;\">+</span>", "<span onmouseover=\"getNotify(this, true)\" onmouseout=\"getNotify(this, false)\" onclick=\"window.open(",$p_get,")\" style=\"float: right; cursor: pointer; padding-right: 15px;\">⬇</span></h2>";


$ p_get是一个URL,window.open()会打开一个窗口。但是,如果我尝试使用它,将会发生以下情况:
javascript - PHP和JS帮助:函数不接受粘贴回显的参数-LMLPHP

和控制台中的JS错误:

Uncaught SyntaxError: missing ) after argument list


我该如何解决?

最佳答案

网址周围似乎有换行(\n)字符。

echo "<h2 style=\"color: white!important; background: black!important; border: 4px black solid!important;\">",
 $p_name, "<span id=\"portfolio-", $p_id, "\" onclick=\"expandToggle(this);\" style=\"float: right; padding-right: 10px; cursor: pointer;\">+</span>",
 "<span onmouseover=\"getNotify(this, true)\" onmouseout=\"getNotify(this, false)\" onclick=\"window.open('",str_ireplace("\n", "", $p_get),"')\" style=\"float: right; cursor: pointer; padding-right: 15px;\">⬇</span></h2>";


更改的部分是:

window.open('",str_ireplace("\n", "", $p_get),"')


更新:
由于您的代码可能难以阅读,因此此处难于理解是一种更好的格式化解决方案。

printf(
    '<h2 style="color: white!important; background: black!important; border: 4px black solid!important;">
        %s
        <span id="portfolio-%d" onclick="expandToggle(this);" style="float: right; padding-right: 10px; cursor: pointer;">+</span>
        <span onmouseover="getNotify(this, true)" onmouseout="getNotify(this, false)" onclick="window.open(\'%s\')" style="float: right; cursor: pointer; padding-right: 15px;">⬇</span>
    </h2>',
     $p_name, $p_id, str_replace("\n", "", $p_get)
);

10-05 20:34
查看更多