【问题标题】:Uncaught SyntaxError: Unexpected token ILLEGAL - exactly the same code works fineUncaught SyntaxError: Unexpected token ILLEGAL - 完全相同的代码可以正常工作
【发布时间】:2013-01-04 10:48:24
【问题描述】:

下面的代码应该将文本区域附加到 div 上,div 的内容进入文本框(<?php echo $section[foo]; ?> 部分)。主要和侧边栏部分按预期工作,但 3 页脚部分会抛出 Uncaught SyntaxError: Unexpected token ILLEGAL 消息。他们使用完全相同的代码,除了 PHP 数组名称(完全复制 + 粘贴)。有什么想法吗?

        <script>
        function textarea(location,text)
        {
            window.alert(location);
            var textarea=document.createElement("textarea");
            var node=document.createTextNode(text);
            textarea.appendChild(node);
            var element=document.getElementById(location);
            element.appendChild(textarea);
        }
        </script>

               <div id="main">
                    <span onclick="textarea(this.parentNode.id,'<?php echo htmlentities($section['main']);?>');" class="edit"><img src="images/edit.png" alt="Edit" /> Edit Section</span>
                    <?php echo $section['main']; ?>
                </div>
                <div id="sidebar">
                    <span onclick="textarea(this.parentNode.id,'<?php echo htmlentities($section['sidebar']);?>');" class="edit"><img src="images/edit.png" alt="Edit" /> Edit Section</span>
                    <?php echo $section['sidebar']; ?>
                </div>

               <div id="left_column">
                    <span onclick="textarea(this.parentNode.id,'<?php echo htmlentities($section['footer_left']);?>');" class="edit"><img src="images/edit.png" alt="Edit" /> Edit Section</span>
                    <?php echo $section['footer_left']; ?>
                </div>
                <div id="right_column_outer">
                    <div id="right_column_left">
                        <span onclick="textarea(this.parentNode.id,'<?php echo htmlentities($section['footer_middle']);?>');" class="edit"><img src="images/edit.png" alt="Edit" /> Edit Section</span>
                        <?php echo $section['footer_middle']; ?>
                    </div>
                    <div id="right_column_right">
                        <span onclick="textarea(this.parentNode.id,'<?php echo htmlentities($section['footer_right']);?>');" class="edit"><img src="images/edit.png" alt="Edit" /> Edit Section</span>
                        <?php echo $section['footer_right']; ?>
                    </div>
                </div>

完整的数组。我没有在这里写 HTML 标记(NicEdit 生成)

array(5) {
  ["footer_left"]=>
  string(365) "<h2 class="article">About</h2>
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sed justo in quam scelerisque pellentesque. Suspendisse erat tellus, sagittis eu venenatis non, eleifend vel arcu. Sed tempus magna sit amet nisi euismod ac cursus turpis faucibus. In eget nulla quam. Donec sagittis facilisis leo at porta. In libero lorem, aliquet vel vehicula a, fringilla ut odio. Praesent tincidunt porta hendrerit. Donec interdum augue eu nunc elementum bibendum. Etiam feugiat rhoncus quam, quis tincidunt justo imperdiet non. Donec bibendum congue arcu. Cras sollicitudin turpis ut tellus vehicula non ornare leo consectetur. Suspendisse dignissim faucibus felis non tempor. Mauris venenatis lorem in turpis facilisis tincidunt. Proin gravida, quam id ultrices ullamcorper, augue dolor porta massa, iaculis rhoncus felis purus quis diam. Morbi ultrices ultricies ipsum id placerat."
  ["footer_middle"]=>
  string(424) "<h2 class="article">Location</h2>
                        <iframe id="location_map" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://www.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=narre+warren&amp;aq=&amp;sll=-37.916385,145.019321&amp;sspn=0.057691,0.132093&amp;ie=UTF8&amp;hq=&amp;hnear=Melbourne+Victoria,+Australia&amp;t=m&amp;z=13&amp;ll=-38.027115,145.30304&amp;output=embed"></iframe>"
  ["footer_right"]=>
  string(294) "<h2 class="article">Contact</h2>
                        <strong>Skype:</strong> <a href="skype:sample?call">sample</a><br />
                        <strong>Phone Number:</strong> 999999999<br />
                        <strong>Email:</strong> <img id="email_address" src="images/email.png" alt="Email Address" />"
  ["main"]=>
  string(66) "<b>Hello. </b><i>This is sam </i><u>Testing this website&nbsp;</u>"
  ["sidebar"]=>
  string(721) "<blockquote style=\"margin: 0 0 0 40px; border: none; padding: 0px;\"><blockquote style=\"margin: 0 0 0 40px; border: none; padding: 0px;\"><blockquote style=\"margin: 0 0 0 40px; border: none; padding: 0px;\"><div style=\"text-align: right;\"><h5><span style=\"font-family: \'courier new\'; font-size: xx-large;\">This should all be right aligned.d</span></h5></div></blockquote></blockquote><blockquote style=\"margin: 0 0 0 40px; border: none; padding: 0px;\"><blockquote style=\"margin: 0 0 0 40px; border: none; padding: 0px;\"><div style=\"text-align: right;\"><h5><span style=\"font-family: \'courier new\'; font-size: xx-large;\">I don\'t think lists work.</span></h5></div></blockquote></blockquote></blockquote>"
}

提前谢谢你。

编辑:这是jsfiddle > 它现在按预期工作(单击“编辑部分”,它应该提醒您父 div id 是什么,然后添加一个文本包含该 div 内容的框是)我已经取出了 PHP...我想知道为什么这在我的网站中不起作用。

【问题讨论】:

  • 如果你把生成的标记+javascript放到jsfiddle会很棒
  • 完成。底部有一个edit
  • 我找不到任何问题,它可以按您的预期工作。发生的唯一错误是一些跨域问题。您是否对更好的解决方案感兴趣(没有 onclick-inline)?如果是这样,我可以将其添加为评论。我需要知道你是否能够使用 jquery,如果不能,你需要支持哪些浏览器(IE)。
  • 我还在梳理,试图找到这个问题;但如果你愿意,那就太好了。我可以使用 jQuery。

标签: javascript dom


【解决方案1】:

所以我更新了你的小提琴。我删除了所有内联 javascript,将 jQuery 和单击处理程序添加到具有“编辑”类的所有跨度。看到我添加了一个带有“.content”类的 div 包装器,以便更轻松地检索 html。

那么你的 js 就这么短:

$(document).ready(function () {
  var $parent;
  $('span.edit').on('click', function () {
    $parent = $(this).parent();
    $parent.append('<textarea>' + $parent.find('.content').html() + '</textarea>');
  });
});

更新的小提琴在这里:http://jsfiddle.net/fXPkQ/1/

【讨论】:

  • 谢谢。 jQuery 是一种可以理解的语言。
猜你喜欢
  • 2012-11-20
  • 2012-01-30
  • 2011-12-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 2012-09-22
  • 1970-01-01
相关资源
最近更新 更多