未捕获的SyntaxError:意外令牌非法-完全相同的代码工作正常

Uncaught SyntaxError: Unexpected token ILLEGAL - exactly the same code works fine

本文关键字:代码 工作 非法 SyntaxError 意外 令牌      更新时间:2023-09-26

下面的代码应该将文本区域附加到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>"
}

提前感谢。

EDIT:这是jsfiddle>它现在按预期工作(单击"编辑部分",它应该提醒你父div id是什么,然后添加一个文本框与该div的内容是),我已经拿出了PHP…我想知道为什么这不是工作在我的网站。

所以我更新了你的小提琴。我删除了所有的内联javascript,添加了jQuery和点击处理程序,所有跨度与类'edit'。看到我添加了一个div-wrapper类'。Content ',以便更容易检索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/