文本区域突出显示魔术词。更改字体大小和颜色。
我想要一个在文本区域输入中突出显示的神奇单词。我在某处找到了几乎可以实现我想要的功能的代码,请参阅下面的链接和Javascript代码:
http://mootools .net/demos/?demo=Element.Event
这里是 javascript 代码
window.addEvent('domready', function() {
var textarea = $('myTextarea'), log = $('log').setStyle('opacity', 0);
// We define the highlight morph we're going to
// use when firing an event
var highlight = new Fx.Morph(log, {
duration: 1500,
link: 'cancel',
transition: 'quad:out'
});
// Here we start adding events to textarea.
// Note that 'focus' and 'keyup' are native events, while 'burn'
// is a custom one we've made
textarea.addEvents({
focus: function() {
// When focusing, if the textarea contains value "Type here", we
// simply clear it.
if (textarea.value.contains('Type here')) textarea.set('value', '');
},
keyup: function() {
// When user keyups we check if there are any of the magic words.
// If yes, we fire our custom event burn with a different text for each one.
if (textarea.value.contains('hello')) textarea.fireEvent('burn', 'hello world!');
else if (textarea.value.contains('moo')) textarea.fireEvent('burn', 'mootools!');
else if (textarea.value.contains('pizza')) textarea.fireEvent('burn', 'Italy!');
else if (textarea.value.contains('burn')) textarea.fireEvent('burn', 'fireEvent');
// note that in case of 'delayed', we are firing the event 1 second late.
else if (textarea.value.contains('delayed')) textarea.fireEvent('burn', "I'm a bit late!", 1000);
},
burn: function(text) {
// When the textarea contains one of the magic words
// we reset textarea value and set the log with text
textarea.set('value', '');
log.set('html', text);
// then we start the highlight morphing
highlight.start({
backgroundColor: ['#fff36f', '#fff'],
opacity: [1, 0]
});
}
});
});
我不想清除文本区域,我希望它通过更改特定单词的字体颜色来突出显示文本区域中的魔术词并增加字体 尺寸。 (只有魔法词,没有别的)
我尝试用 JSFIDDLE 进行实验,但仍然无法让它工作。
i want a magic word highlighing in textarea input. I found code somewhere that almost does what I want see link and Javascript code below:
http://mootools.net/demos/?demo=Element.Event
here is the javascript code
window.addEvent('domready', function() {
var textarea = $('myTextarea'), log = $('log').setStyle('opacity', 0);
// We define the highlight morph we're going to
// use when firing an event
var highlight = new Fx.Morph(log, {
duration: 1500,
link: 'cancel',
transition: 'quad:out'
});
// Here we start adding events to textarea.
// Note that 'focus' and 'keyup' are native events, while 'burn'
// is a custom one we've made
textarea.addEvents({
focus: function() {
// When focusing, if the textarea contains value "Type here", we
// simply clear it.
if (textarea.value.contains('Type here')) textarea.set('value', '');
},
keyup: function() {
// When user keyups we check if there are any of the magic words.
// If yes, we fire our custom event burn with a different text for each one.
if (textarea.value.contains('hello')) textarea.fireEvent('burn', 'hello world!');
else if (textarea.value.contains('moo')) textarea.fireEvent('burn', 'mootools!');
else if (textarea.value.contains('pizza')) textarea.fireEvent('burn', 'Italy!');
else if (textarea.value.contains('burn')) textarea.fireEvent('burn', 'fireEvent');
// note that in case of 'delayed', we are firing the event 1 second late.
else if (textarea.value.contains('delayed')) textarea.fireEvent('burn', "I'm a bit late!", 1000);
},
burn: function(text) {
// When the textarea contains one of the magic words
// we reset textarea value and set the log with text
textarea.set('value', '');
log.set('html', text);
// then we start the highlight morphing
highlight.start({
backgroundColor: ['#fff36f', '#fff'],
opacity: [1, 0]
});
}
});
});
I don't want to clear the textarea, I want it to highlight the magic word within the textarea by changing the font colour of that particular word and increasing the font size. (only of the magic word, nothing else)
I tried to experiment with JSFIDDLE but stil couldnt get it to work.
http://jsfiddle.net/api/post/mootools/1.4/dependencies/more/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为可以更改颜色或以任何方式将特定样式应用于文本区域中的文本。
话虽这么说,您可以用 div 替换文本区域,您可以在用户键入时将文本附加到该 div 上。当您检测到关键字时,只需将标签包裹在 div 中的文本周围即可。如果这样做,您可能需要为 div 提供 tabindex 属性,因为这将允许它获得焦点。然后您可以在插入文本之前检查该 div 是否被选中。
如果你确实想使用文本区域进行输入,那么你也可以尝试在文本区域上浮动一个div或span元素,然后让你的burn函数设置浮动div的文本,并使用CSS将其定位在用户的单词上键入。您可以通过以下方式确定光标在文本区域中的位置:
I don't believe it is possible to change the color or in any way apply specific styling to text in a textarea.
That being said, you can replace the textarea with a div, which you can append text to as the user types. When you detect a keyword, it is a simple task of wrapping an tag around the text in the div. If you do this, you may want to give the div a tabindex property, as this will allow it to have focus. Then you can check whether the div is selected before inserting text into it.
If you really want to use a textarea for input, then you can also try floating a div or span element over the textarea, and then have you burn function set the text of floating div, and use CSS to position it over the word the user typed. You can determine where the cursor is in a textarea with something like: