使用 PHP 时,Onclick Word Replacement 无法正常工作

Onclick Word Replacement not working correctly with PHP

我有这个代码:

<a class='show' data-id='".$random."' href='javascript:void(0)' onclick='if(this.innerHTML == 'Click Here to Read More'){this.innerHTML = 'Click Here to Read Less'}else{this.innerHTML = 'Click Here to Read More'}'>Click Here to Read More</a>

当返回页面时,虽然返回的一切都很奇怪,并且源代码中缺少项目,如下所示:

<a class="show" onclick="if(this.innerHTML == " href="javascript:void(0)" data-id="23272317" }'="" }else{this.innerhtml="Click Here to Read More" more'){this.innerhtml="Click Here to Read Less" read="" to="" here="" click="">Click Here to Read More</a>

我确信它就像引号问题一样简单,但我卡住了。任何人都能够指出我正确的方向或解释如何或为什么只有 javascript?

我想你用 echo:

输出这段代码
echo "<a class='show' data-id='".$random."' href='javascript:void(0)' onclick='if(this.innerHTML == \"Click Here to Read More\"){this.innerHTML = \"Click Here to Read Less\"} else {this.innerHTML = \"Click Here to Read More\"}'>Click Here to Read More</a>";

请注意,我在 Click Here to ... 周围替换了 ' 以转义 ",因为:

  • ' 已用于封装属性数据
  • " 附上您 echo
  • 的完整字符串