dongqing8765 2013-03-15 23:08
浏览 49

将带有撇号的格式化文本从javascript传递到带有innerHTML的html元素

I have 3 pages that load the same slideshow. Am trying to consolidate to one page by passing new content in a variable from javascript to an html element. Effect should be similar to ajax. Passed content includes html markup. All works fine, but if I include an apostrophe symbol in the text, it fails. I can work around by using the acute symbol; but would like to use the apostrophe. Here's a sample of the markup and script. Same result in firefox, chrome and ie9. Not a big deal, but I´d like to understand why it fails. Any ideas?

<?php
// Test of script passing content to <p> element
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
    <head>
<script type="text/javascript">
function testSwap(text) {
    var text1=text;
    document.getElementById('test').innerHTML = text1;
}
</script>
</head><body>
<?php
$testtext="&lt;img style= &quot; float:left;margin:10px &quot; src= &quot;          http://www.w3schools.com/images/pulpit.jpg &quot; alt=&quot; Pulpit rock &quot; width=&quot;50&quot; height=&quot;60&quot; /&gt; &lt;span style=&quot;font-weight:bold;font-size:16px;text-decoration:underline&quot; &gt; Second Page Title &lt;/span&gt; : This page tells how we found our family&#180;s origins. This sample includes an image, a span element with styling, and a double line break. &lt;br /&gt;&lt;br /&gt; They all work. Unfortunately, the script fails if I include an apostrophe in this text so I use &#180; instead.&lt;br /&gt;&lt;br /&gt;Same result in firefox, chrome and ie9. Not a big deal, but I&#180;d like to understand why it fails. Any ideas?";


?>
<div><button type="button" onclick="testSwap('<?php echo $testtext; ?>')">Click Me!</button></div>

<p id='test'><span style='font-weight:bold;font-size:16px;text-decoration:underline'>My Main Page Title:</span>
The pages are in a genealogy site with our family&#39;s history. Main page introduces site and describes content<br /><br />Other two pages use same slide show of family pictures, but have more detailed info.<br /><br />Menu currently directs to a new page and reloads the slideshow. I plan to use javascript to avoid the reload if user has it enabled.<br /><br />This sample demonstrates the idea. Click on the button for second page.
</p>

</body></html>
  • 写回答

2条回答 默认 最新

  • dongou5100 2013-03-15 23:12
    关注

    Use htmlentities: http://php.net/manual/en/function.htmlentities.php

    onclick="testSwap('<?php echo htmlentities($testtext); ?>')"
    

    Your $testtext is what has the quote in it, correct? If so, htmlentities will turn the quote into a &#039; (or &#34; if it's a double quote), and it won't break the testSwap onclick.

    Edit: I'm sorry, I'm completely changing my answer. Use htmlentities instead (it was urlencode before).

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?