下面的代码应该将textarea附加到div上,div的内容将进入文本框(<?php echo $section[foo]; ?>
部分)。主要&amp;侧栏部分按预期工作,但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&source=s_q&hl=en&geocode=&q=narre+warren&aq=&sll=-37.916385,145.019321&sspn=0.057691,0.132093&ie=UTF8&hq=&hnear=Melbourne+Victoria,+Australia&t=m&z=13&ll=-38.027115,145.30304&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 </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&gt;它现在按预期工作(点击'编辑部分',它应该提醒你父div是什么,然后添加一个文本框,其中包含该div的内容),我已经取出了PHP。 ..我想知道为什么这不适用于我的网站。
答案 0 :(得分:1)
所以我更新了你的小提琴。我删除了所有内联javascript,添加了jQuery和一个点击处理程序到所有跨度的“编辑”类。看到我添加了一个带有'.content'类的div-wrapper,以便更容易检索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/