用户选择文本并将其替换为'a'标签id ='NewLink',当他完成单词或短语的描述后,服务器将获得内容div'StartId'的innerHTML。我需要忽略的是,用户不会在浏览器的客户端上更改html代码。我该怎么办?

  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>

最佳答案

将您的$.post代码更改为此:

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

您的参数未正确序列化。

如果htmlInner是整数,则可以将参数更改为此:
'{"htmlInner": '+ StartId + '}'

关于javascript - 如何将原始的innerHTML传递给php?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43573311/

10-11 13:35