1

我有一个轻微的程序问题,

我有一个基于 php 的服务器。基本上我向服务器 php 文件( server.php 文件)发送一个字符串,这个 php 文件对该字符串执行一些操作,然后返回新字符串。

我想在 server.php 文件中添加一些 js,它将在字符串上运行一些 jquery dom 遍历函数。该字符串是一个 DOM。

我知道这不是 js 的设计初衷,我知道它应该只在浏览器上运行,但是我可以做一个简单的解决方法吗?

字符串看起来像这样:

<body>      
<em>
    heres a em and this works as well

</em>
<p>
   here is another para and it works , BUT NOW LOOK AT THE NEXT LINE
    <br>
       where is this prepend,no prepend here, ladies and gentlement,,NADA.

</p>
<button onclick = "addtags()">here is the magic</button>

 </body>

所以我需要通过 php 做一些事情,然后进行一些额外的树遍历,我喜欢 jquery 函数,我正在使用 .text() 、 .contains() 、 .before() , .after(), .attr('class') 来自 jquery 的函数,如果有人可以提出任何替代方案,我觉得很好.. 这一切都可以在 XPATH 中完成吗?我不知道.. 对我来说 jquery 完美。我是否需要从我收到的字符串中创建一个新的 .html 文件并以某种方式在其上运行 js?

最好的问候,理查德。

4

2 回答 2

1

您不能在服务器上同时运行 js 和 php。理论上可以完成,但不能在同一个执行流程中完成。

您可以在 php.ini 中进行 DOM 处理。

于 2013-02-16T21:26:09.293 回答
0

You can use PHP to echo javascript code to run on the client, but you cannot intermingle javascript and PHP in terms of execution on the server (unless you use some sort of interpreter like v8js).

Why can't you traverse your strings with PHP code? If you really want to use javascript then use jquery to make an ajax call to your PHP file. Then manipulate the returned string in jquery however you need to.

于 2013-02-16T21:26:46.190 回答