我从9lessons.info获得了这个脚本,当你输入@ simbol时它应该自动建议朋友。它很棒!但它使用启用内容盒的div作为文本框,但由于这是一个HTML5功能,但我需要一个更兼容的解决方案,如文本区域。但是textarea不使用当前的jQuery,即使ID与div相同。我对jQuery不好,所以有人能看出原因吗?
源代码:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Facebook like Tag Friends</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
var start=/@/ig;
var word=/@(\w+)/ig;
$("#contentbox").live("keyup",function()
{
var content=$(this).text();
var go= content.match(start);
var name= content.match(word);
var dataString = 'searchword='+ name;
if(go.length>0)
{
$("#msgbox").slideDown('show');
$("#display").slideUp('show');
$("#msgbox").html("Type the name of someone or something...");
if(name.length>0)
{
$.ajax({
type: "POST",
url: "boxsearch.php",
data: dataString,
cache: false,
success: function(html)
{
$("#msgbox").hide();
$("#display").html(html).show();
}
});
}
}
return false();
});
$(".addname").live("click",function()
{
var username=$(this).attr('title');
var old=$("#contentbox").html();
var content=old.replace(word,"");
$("#contentbox").html(content);
var E="<a class='red' contenteditable='false' href='#' >"+username+"</a>";
$("#contentbox").append(E);
$("#display").hide();
$("#msgbox").hide();
$("#contentbox").focus();
});
});
</script>
</head>
<body>
<h3>Tutorial link <a href="http://9lessons.info">Click Here</a></h3>
<h2>Eg: 9lessons blog @sri</h2>
<div id="xxx"></div>
<div id="container">
<div id="contentbox" contenteditable="true">
</div>
<div id='display'>
</div>
<div id="msgbox">
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
尝试:
var html = $('#contentbox').html();
var textarea = $('<textarea />').attr('id', 'contentbox').html(html);
$('#contentbox').replaceWith(textarea);