PHP和JS帮助:函数不接受用echo粘贴的参数

时间:2015-09-19 11:20:13

标签: javascript php

这是我用来通过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(),打开一个窗口。然而,如果我尝试使用它会发生这种情况: nothing to see here

和控制台中的JS错误:

Uncaught SyntaxError: missing ) after argument list

我该如何解决?

1 个答案:

答案 0 :(得分:2)

网址周围似乎有新的行(\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)
);