简体   繁体   English

如何将原始的innerHTML传递给php?

[英]How to pass original innerHTML to php?

A user selects text and it replaces with 'a' tag id='NewLink', when he'll has finished with description for a word or phrases the server will get innerHTML of content div 'StartId'. 用户选择文本并替换为'a'标签id ='NewLink',当他完成对一个或多个单词的描述时,服务器将获得内容div'StartId'的innerHTML。 I need to overlook that a user doesn't change the html code on a client side in browser. 我需要忽略用户不会在浏览器中更改客户端的html代码。 How I can do it? 我怎么能这样做?

  function GetSelectedText () { var sel = window.getSelection(); if( sel.toString().length != 0 && sel.getRangeAt) { $('#NewLink').replaceWith($('#NewLink').html()); var rng, se, err, errt, patt; errt = 0; try { se=$('<span style="background-color: #3366CC;" id="NewLink">')[0]; rng=sel.getRangeAt(sel.rangeCount-1); rng.surroundContents(se); rng.selectNode(document.getElementById("NewLink")); } catch (err) { errt = 1;} if(errt != 1) { patt=new RegExp("</?a.*?>","g"); if (patt.test($("#NewLink").html()) ||$("#NewLink").closest("a").length||!$("#NewLink").text().replace(/\\s/g,"").length) {$("#NewLink").replaceWith($("#NewLink").html()); } else { rng.collapse(!0);} } } var StartId = document.getElementById("StartId").innerHTML; $.post('site.com', {htmlInner:StartId}, function(data) { alert(data); }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="StartId" onmouseup="GetSelectedText ()"> Carnotaurus, a large theropod dinosaur, lived during the Late Cretaceous period. Known from a single well-preserved skeleton found in Argentina, it is a member of the Abelisauridae family<a href="#">and one of the best-understood theropods from the Southern Hemisphere. <br>Carnotaurus (derived from Latin for "meat-eating bull") <br> had thick horns above the eyes, and a very deep skull on a muscular neck.</a> <br>It was a lightly built, bipedal predator, 8 to 9 m (26.2 to 29.5 ft) long, weighing at least 1.35 metric tons (1.49 short tons). It had small, vestigial forelimbs and long and slender hindlimbs. Preserved skin impressions show a mosaic of small scales interrupted by large bumps that lined the sides of the animal. </div> <div id="EndId"></div> 

change your $.post code to this: 将您的$.post代码更改$.post

$.post('site.com', '{"htmlInner":"' + StartId + '"}', function(data) {
       alert(data);
    });          
} 

your parameter not properly serialized. 您的参数未正确序列化。

if your htmlInner is an integer, you can change the parameter to this: 如果您的htmlInner是一个整数,您可以将参数更改为:

'{"htmlInner": '+ StartId + '}'

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

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