08.02.2012, 23:43
|
#3
|
ПроЭктировщик
Регистрация: 19.02.2011
Сообщений: 134
Написано 81 полезных сообщений (для 219 пользователей)
|
Ответ: Gadget самописка
Не хотелось бы казаться грубым, но ваш код немного дерьмо.
Написал пример:

<html>
<head>
<title>Cat knows what is good for you.</title>
<style type="text/css">
body {
font-family: Verdana, Arial, Sans;
font-size: 12px;
}
#container {
width: 480px;
}
#lines {
padding: 4px;
background: #f7f7f7;
border: 1px solid #ccc;
}
#input {
width: 450px;
padding: 2px;
}
#inbtn {
float: right;
width: 25px;
}
.line {
margin-top: 4px;
padding-left: 2px;
border-left: 3px solid #eee;
}
.line_neutral { border-left-color: #ccc; }
.line_user { border-left-color: #d87; }
.line_system { border-left-color: #78e; }
</style>
</head>
<body onLoad="init();">
<div id="container">
<div id="lines"></div>
<button type="button" id="inbtn" onClick="user_input();">></button>
<input type="text" id="input" onChange="user_input();">
</div>
<script type="text/javascript">
var lines = []; // pointers to text <div>'s
var lines_el = document.getElementById('lines'); // main text <div>
var input_el = document.getElementById('input'); // input field
var questions = [
// questions to be asked randomly:
"What's your name?",
'Do you like single-quoted strings?',
'Insert more questions here, okay?'
];
function random(n) { return Math.floor(Math.random() * n); }
function line_onclick(event) {
lines_el.removeChild(this);
lines.splice(lines.indexOf(this), 1); // remove this element from lines array
}
function line_add(text, type) {
var el = document.createElement('div'); // create a new div
el.innerHTML = text; // set contents
el.onclick = line_onclick; // bind click event
if (type === undefined) type = 'neutral'; // default type if not specified
el.className = 'line line_' + type; // set <div> class property
lines_el.appendChild(el); // add it to lines containers
lines.push(el); // add it to list
}
function feedback(text) {
// convert to lowercase, for easier processing:
text = text.toLowerCase();
// handle input text here:
if (text == 'hi' || text == 'hello') return 'How are you?';
// return random question:
return questions[random(questions.length)];
}
function user_input() {
var text = input_el.value; // get input
line_add(text, 'user'); // add user input line
line_add(feedback(text), 'system'); // add answer line
input_el.value = ''; // reset input text
}
function init() {
lines_el.innerHTML = ''; // reset contents of lines box
line_add('Example by YellowAfterlife');
line_add('Hello, user', 'system');
}
</script>
</body>
</html>
Если нужно по Enter делать действия, то нужно привязать onkeydown событие (keydown для старых IE).
__________________

Мой сайт-блог. Игры, обновления, примеры для Haxe, JavaScript(+HTML5), GameMaker, Love2d...
|
(Offline)
|
|