我想为textarea创建预览。
这是一张关于它的外观和实际问题的图片。
我的问题是我的bb代码不像我想的那样工作。我知道bb代码可以工作,因为它可以在论坛中运行,但在这个预览功能中却没有。只有[“b”]和[“i”]标签正常工作。
所以我猜可能会说代码因为新的span或div而在其他代码中断,我不知道为什么。 我还没有找到解决方案,如果有的话,这就是整个代码:
new_reply.inc:
<script>
function toggle_hide_show()
{
var gettext = document.getElementById('message').value;
var inputpost = document.getElementById('addtopost');
inputpost.style.display = inputpost.style.display == 'none' ? 'block' : 'none';
var vorschau = document.getElementById('show_text');
vorschau.style.display = vorschau.style.display == 'none' ? 'block' : 'none';
var chang_button= document.getElementById('change_button')
chang_button.style.display = chang_button.style.display == 'none' ? 'block' : 'none';
var id=$(message).val();
var dataString = 'message='+ id;
$.ajax
({
type: "POST",
url: "ajax_bbcode.php",
data: dataString,
cache: false,
success: function(html)
{
$(".text").html(html);
}
});
return false;
}
</script>
<div id="addtopost">
<img src="b.png" onclick="formatText ('u');" />
<img src="i.png" onclick="formatText ('i');" />
<img src="center.png" onclick="formatText ('center');"/>
<textarea name="message" id="message" cols="7" rows="6"></textarea>
<input type="button" onclick="toggle_hide_show()" value="preview"/>
</div>
<span id="show_text" class="text" style="display:none;"></span>
<input id="change_button" type="button" onclick="toggle_hide_show()" value="back" style="display:none;"/>
</div>
然后是ajax_bbcode.php
<?php
include "bbcode_function.inc";
if($_POST['message'])
{
$message = $_POST["message"];
if(get_magic_quotes_gpc())
{
$message = stripslashes($message);
}
$message = mysql_real_escape_string(bbcode_to_html($message));
echo $message;
}
?>
最后但并非最不重要的是bbcode_function.php:
<?php
//This function let convert BBcode to HTML
function bbcode_to_html($text)
{
$text = nl2br(htmlentities($text, ENT_QUOTES, "UTF-8"));
$in = array(
"#\[b\](.*)\[/b\]#Usi",
"#\[u\](.*)\[/u\]#Usi",
"#\[center\](.*)\[/center\]#Usi"
);
$out = array(
"<strong>$1</strong>",
"<span style=\"text-decoration:underline;\">$1</span>",
"<div style=\"text-align:center;\">$1</div>"
);
$count = count($in)-1;
for($i=0;$i<=$count;$i++)
{
$text = preg_replace($in[$i],$out[$i],$text);
}
return $text;
}
//This function let convert HTML to BBcode
function html_to_bbcode($text)
{
$text = str_replace("<br />","\n",$text);
$in = array(
"#<strong>(.*)</strong>#Usi",
"#<span style=\"text-decoration:underline;\">(.*)</span>#Usi",
"#<div style=\"text-align:center;\">(.*)</div>#Usi"
);
$out = array(
"[b]$1[/b]",
"[u]$1[/u]",
"[center]$1[/center]"
);
$count = count($in)-1;
for($i=0;$i<=$count;$i++)
{
$text = preg_replace($in[$i],$out[$i],$text);
}
return $text;
}
?>
编辑:对不起第一个人误导标题,错字:(