I got this script off 9lessons.info and it is supposed to auto suggest friends when you type an @ simbol. It works great! But it uses a contentbox enabled div as a text box, but as this is a HTML5 feature but i need a more compatible solution like a text area. But a textarea isnt working with the current jQuery, even with the same ID as the div. I am not good with jQuery so can anyone see why?
Source Code:
<!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();
$("#co开发者_运维问答ntentbox").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>
Try:
var html = $('#contentbox').html();
var textarea = $('<textarea />').attr('id', 'contentbox').html(html);
$('#contentbox').replaceWith(textarea);
See: http://jsfiddle.net/yFKRX/1/
精彩评论