简体   繁体   English

jQuery的replaceWith()方法将PHP代码更改为HTML注释

[英]jQuery's replaceWith() method changes PHP code to HTML comment

In a click event handler I would like to change a div element. click事件处理程序中,我想更改div元素。 In this element is also HTML and PHP code. 该元素中还有HTML和PHP代码。

After the change the PHP code is in a HTML comment. 更改后,PHP代码在HTML注释中。 What I can do to fix this? 我该如何解决?

在此处输入图片说明

Here is a short version of my code: 这是我的代码的简短版本:

$("#replaceCat1").click(function(){
  $("div.boxcontent2").replaceWith("<div class='boxcontent1'> <?php print_string('placehoder1', 'theme_'.$theme->name); ?></div> ");
});

Edit: 编辑:

i design a theme. 我设计一个主题。 In this theme gives it 4 boxes with information from the backend. 在此主题中,为它提供4个带有后端信息的框。 My php code is at the moment only a placeholder. 我的PHP代码目前仅是一个占位符。 later I include the backend php code. 稍后,我将包含后端php代码。 In my theme, gives a dropdown menu. 在我的主题中,提供一个下拉菜单。 In this gives 4 categories, when you select one, then I would like that the 3 of the 4 boxes switch die information and the design. 在这给出了4个类别中,当您选择一个类别时,我希望4个方框中的3个可以切换模具信息和设计。 That is the plan with the code. 那就是带有代码的计划。

The code comes from the .js file. 该代码来自.js文件。 It is better to load the code from a php file and include it with .load() ? 最好从php文件中加载代码,并将其包含在.load()中?

If you want javascript to be able to dynamically include something that comes from php, then you will need to look into AJAX . 如果您希望javascript能动态包含来自php的内容,则需要研究AJAX Probably jQuery's $.ajax() function will be enough. jQuery's $.ajax()函数可能就足够了。

If you are simply wanting the text from PHP to appear once in the javascript, you will need to run the javascript through php. 如果您只是希望PHP中的文本在javascript中出现一次,则需要通过php运行javascript。

Assuming that you actually have a php server an that your project is running on this server, a quick, ugly hack that will allow you to do this is to rename your .js file into a .php file, and include() it between a pair of script tags. 假设你确实有一个您的项目在此服务器,一个快速的,丑陋的黑客攻击,让你做,这是重命名上运行PHP服务器.js文件转换成.php文件, include()这之间一对脚本标签。

<script>
Include('filthy_hack.php');
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM