处理jQuery函数中的单引号和双引号

时间:2019-05-16 06:07:03

标签: php jquery

我从foreach循环中创建了一个链接,因为我在jquery函数中显示了一个具有不同参数的链接

<?php
  foreach ($questions as $row) {
    if (!empty($row['url_ImageName'])) {
       $url_ImageName = $row['url_ImageName'];      
    }else{
    $Paragraph =  $row['Paragraph'];
    }
    ?>
      <a href="#" id="opendetail" onclick="question_details('<?php echo $url_ImageName; ?>','<?php echo $Paragraph; ?>')">Show Details</a>
    <?php
}  ?>

function question_details(url_ImageName,Paragraph){
	if (url_ImageName != '') 
	{
		$(".exam-slideout .question-details img").attr("src",url_ImageName);				
	}
	if (Paragraph != '') 
	{
		$('.exam-slideout .question-details div').html(Paragraph);
	}
	
}

在创建此链接的​​第一个链接中

<a href="#" id="opendetail" onclick="onclick="question_details('','“ What the deuce is it to me ” ? he interrupted impatiently: “ you say that we go round the sun. If we went round the moon it would not make a pennyworth of difference to me or to my work.”')">Show Details</a>

和第二个链接,创建此链接:

<a href="#" id="opendetail" onclick="question_details('','Complete drying is essential before adding the sealant so that the paint doesn't smear.')">Show Details</a>

那,我遇到了单引号和双引号的问题。

要解决此问题,请尝试

$Paragraph = mysqli_real_escape_string($con, $row['Paragraph']);

但是功能仍然无法解决语法错误。

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

只需在字符串中间使用的'之前添加转义字符(\),例如:

<a href="#" id="opendetail" onclick="question_details('','Complete drying is essential before adding the sealant so that the paint doesn\'t smear.')">Show Details</a>

使用模板文字的替代方法:

<a href="#" id="opendetail" onclick="question_details('',`Complete drying is essential before adding the sealant so that the paint doesn't smear.`)">Show Details</a>

您可以了解有关Working with Strings in JavaScript的更多信息。

答案 1 :(得分:0)

您可以在$Paragraph变量上使用addslashes(),这会将'逃逸到\'中。它也将逃脱",所以要小心一点。

<a href="#" id="opendetail" onclick="question_details('<?php echo $url_ImageName; ?>', '<?php echo addslashes($Paragraph); ?>')">Show Details</a>

或者,使用str_replace()'的所有出现替换为\'

<a href="#" id="opendetail" onclick="question_details('<?php echo $url_ImageName; ?>', '<?php echo str_replace("'". "\'", $Paragraph); ?>')">Show Details</a>