dongzhisang5342 2011-10-24 18:25
浏览 49
已采纳

在PHP生成的JavaScript中调用时,通过heredoc声明的PHP变量将无法正确解析

The variable $a below does not seem to parse properly when if I attempt to declare it using a heredoc. It however, does work when I define it with the simple = declaration method. I would like to be able to define it as a heredoc, because a lot of HTML code must be called all at once, and in this example I have just simplified it for demonstrative purposes.


$a = "<a href = \'http://www.google.com\'>Google</a>";

echo "
  <div id = \"test\" 
      ondblclick = \"document.getElementById('test').innerHTML = '$a';\">
        Change Event
  </div>
"; 

// when the user clicks the text "Change Event", 
// it should turn into whatever $a is, in this case a link to Google

The previous code works, however when I try to convert it to a heredoc, something does not parse correctly.


I have tried using four kinds of quote styles, ", \", ', and \'

\' is the quote style required by where the variable lies in the code, within JavaScript, as the other three styles have already been used, and it is the quote that works (obviously) when I am declaring a variable in a simple (=) way.

When I use " or \" the code yields:

Screenshot

When I use ' or \' the code yields:

Screenshot2

In both cases, the ondblclick functionality does not work either. I am not sure what is going wrong and why heredoc is not parsing the way it seems like it should.


Here is one of my attempt at making the heredoc; the only difference between my attempts are the quote styles around the link.

$a = <<<EOT

    <a href = \'http://www.google.com\'>Google</a>

EOT;

// as stated above, I have tried ", \", ', and \'
  • 写回答

4条回答 默认 最新

  • douju7245 2011-10-24 19:19
    关注

    Hmmm trying to think of a way around this whilst still using heredoc... the double quotes within .innerHTML must be escaped when printed to the browser, or you get the problem you have. Some browsers will be kind and automatically escape them but on the whole they won't and, if truth be told, they shouldn't.

    The only way I can get this code to work is by removing the "" from the href attribute completely.

    It's really not recommended to have .innerHTML used this way as the <a> tag now contains technically invlalid HTML, but it seems to work in Firefox,IE 9 (quirks mode on and off) and Chrome.

    $a = <<<EOT
    <a href=http://www.google.com>Google</a>
    EOT;
    $b = <<<EOT
    <div id="test" ondblclick="document.getElementById('test').innerHTML = '$a';">Change Event</div>
    EOT;
    echo $b;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0