简体   繁体   English

转义单引号jQuery HTML

[英]Escape single quote jQuery HTML

I have an ajax function calling a PHP class which should return the code in $var. 我有一个调用PHP类的ajax函数,该类应返回$ var中的代码。 Because of the single quotes in the $var string, JavaScript keeps giving back an error. 由于$ var字符串中的单引号,JavaScript会不断返回错误。

What would be the best way to send code with ajax without worrying about the characters and the number of lines in the code? 用ajax发送代码而不用担心代码中的字符和行数的最佳方法是什么?

$var = ' <span onclick="propMove(\'54957_363807.jpg\',\'7242\',\'right\');" class="badge badge-grey" style="float:right;margin:5px;cursor:pointer"><i class="fa fa-arrow-circle-right"></i></span> ';

<script>   $("#container .content").html(' <?=json_encode($picsIn)?>  ');    </script>

Try the following: 请尝试以下操作:

$var = htmlentities($var);

This will return everything as html characters, and will not contain quotes. 这将以html字符的形式返回所有内容,并且不包含引号。

In jQuery then do the following to the ajax response 在jQuery中,然后对ajax响应执行以下操作

function decodeEntities(encodedString) {
    var textArea = document.createElement('textarea');
    textArea.innerHTML = encodedString;
    return textArea.value;
}

var myCodeFromPHP = decodeEntities(myAjaxResponse);

Where "myCodeFromPHP" is the clean proper HTML, and "myAjaxResponse" is the htmlentities response 其中“ myCodeFromPHP”是干净正确的HTML,“ myAjaxResponse”是htmlentities响应

Hopefully this helps 希望这会有所帮助

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

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