Onclick Word替换不适用于PHP

时间:2019-03-09 12:10:39

标签: javascript php quotes

我有此代码:

<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会如何或为什么发生这种情况?

1 个答案:

答案 0 :(得分:1)

我想您用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
  • 的完整字符串