【问题标题】:How to convert html code to php [duplicate]如何将html代码转换为php [重复]
【发布时间】:2023-03-23 12:46:01
【问题描述】:

我希望将带有js的按钮链接从html转换为php

我在 php 中编写此代码时添加了单击功能,但 onclick 功能不起作用

HTML

<button onclick="document.getElementById('id01').style.display='block'" style="width:auto;">

</button>

Corephp

<?php 
    echo '<button onclick="document.getElementById("id01").style.display="block" " style="width:auto;">';
?>

 This php echo line not working that means that onclick function.

【问题讨论】:

  • onclick="document.getElementById("id01").style ... 尝试计算引号,您将需要转义一些(或突破 PHP 或使用 HEREDOC 语法) - 我怀疑如果您查看源代码,您会看到损坏的HTML 到位。
  • 虽然了解客户端和服务器端之间的区别很有用,但我认为这不是解决这个问题的好副本。
  • @Karl-AndréGagnon 有跑题的风险,你能推荐另一个吗?是我经常遇到的。哦,等等,nvm,就像“糟糕的关闭”一样。见我的言论。过失。
  • @ficuscr 这个可能更好:stackoverflow.com/questions/2109583/…。我已经用onclick 而不是title 测试了这个解决方案,它可以工作。
  • 甚至是您在答案中发布的那个。

标签: javascript php html


【解决方案1】:

啊,我明白了。这是一个逃避问题。抱歉,我的第一条评论错过了标记(将其视为客户端/服务器端断开连接)并认为我劫持了您的问题。

<?php 
    echo '<button onclick="document.getElementById(\"id01\").style.display=\"block\" " style="width:auto;">';
?>

这些可能会有所帮助:

Pass a PHP string to a JavaScript variable (and escape newlines)

How to escape string from PHP for javascript?

【讨论】:

  • @Kalai,如果您仍然卡住,请大声喊叫。
  • 我不明白。然后我将双引号更改为单引号,如下所示 echo '
猜你喜欢
  • 2016-12-16
  • 1970-01-01
  • 2013-08-11
  • 2021-10-03
  • 2015-01-03
  • 2018-11-23
  • 2012-12-05
  • 2012-12-24
  • 2013-02-24
相关资源
最近更新 更多