dpb56083 2015-04-05 04:36
浏览 28
已采纳

html和echo语句中的字符串连接

So I have this line of code in a php file:

echo "<a class='button' onclick=report('$row[id]')></a>";

I'd like to add an id to the line, so I tried this:

echo "<a class='button' id='"report".$row[id]' onclick=changeColor('$row[id]')></a>";

and in my javascript file

document.getElementById('report'+id).style.color('blue');

but I keep getting errors in javascript

cannot read property 'style' of null,

because I'm doing something wrong with the concatenation. I'm trying to get the id to be something like "report100" by concatenating it with $row[id] and then in the javascript file, change it's font color. I've tried other variations, but I just seem to get it right.

  • 写回答

2条回答 默认 最新

  • dqyym3667 2015-04-05 04:40
    关注

    try like this.

    echo "<a class='button' id='report".$row['id']."' onclick=changeColor(".$row['id'].")></a>";
    

    UPDATE :

    To concatenate a string and variable together, use the string concatenation operator . (a dot).

    As well it is depend on how you start the string

    Starting the string with '' (single quotes) will be

    echo '<a class="button" id="report'.$row['id'].'">';
    

    Starting the string with "" (double quotes) will be

    echo "<a class='button' id='report'".$row['id'].">";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料