简体   繁体   English

PHP-如何在元素上获取innerHTML?

[英]PHP - How I can get innerHTML on an element?

I am novice in PHP. 我是PHP的新手。 I have javascript functions for adding , removing and moving favorites (DOM Manipulation only). 我具有用于添加,删除和移动收藏夹的javascript函数(仅适用于DOM操作)。 When I refresh the page this favorites are cleared. 当我刷新页面时,此收藏夹将被清除。 I need: 我需要:

1.Get innerHTML on current (DOM manipulated) favorites DIV element (where favorites are). 1.在当前(DOM操作)收藏夹DIV元素(收藏夹所在的位置)上获取innerHTML。

2.Store the HTML in PHP varable (i need 'php' varable cuz i will insert the value in DB). 2.将HTML存储在PHP变量中(我需要'php'变量cuz,我将在DB中插入值)。

Favorite DIV with one favorite created : 收藏夹DIV创建了一个收藏夹:

<div id="favorites">
    <div id="1" class="favorite">
       <sub class="minID">Id 1</sub>
       <a href="http://www.zamunda.net">Zamunda.NET</a>
       <span onclick="movefavorite(1)">
          <img class="move" src="icon/move.png" title="Move">
       </span>
       <span onclick="removefavorite(1)">
           <img class="delete" src="icon/del.png" title="Delete">
       </span>
    </div>
</div>

I would do one of the following, depending on what you're really trying to accomplish. 根据您实际要完成的工作,我将执行以下操作之一。

  1. Do an ajax post to your PHP code that contains the information required to save the favorite entries (ie just the ID, URL and name, not the actual HTML). 在您的PHP代码中进行ajax发布,其中包含保存收藏夹条目所需的信息(即,仅ID,URL和名称,而不是实际的HTML)。
  2. Maintain a few hidden form fields that hold the information required to save the favorite entries. 维护一些隐藏的表单字段,其中包含保存收藏夹条目所需的信息。 This will give you access to the data from PHP with the form is submitted (assuming a form is involved). 这将使您可以在提交表单后(假设涉及表单的情况下)从PHP访问数据。

Note that I don't think what you really want is to get the innerHTML of elements from your PHP code. 注意,我不认为您真正想要的是从PHP代码中获取元素的innerHTML。 You probably want to find a way to represent that information in a way that can more easily be sent to your server-side code. 您可能希望找到一种方式来表示该信息,从而可以更轻松地将其发送到服务器端代码。

js: js:

var v = document.getElementsByClassName('minID')[0].innerHTML; // select whatever
$.cookie("something", v, { expires: 5 }); // set cookie

php: 的PHP:

<?php
     echo $_COOKIE['something'];
     /* ... */
?>

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

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