mootools
Comment previewing is an awesome addition to any blog. I've seen really simple comment previewing and some really complex comment previewing. The following is a tutorial on creating very basic comment previewing using MooTools.
评论预览是对任何博客的补充。 我看过非常简单的评论预览和一些非常复杂的评论预览。 以下是有关使用MooTools创建非常基本的注释预览的教程。
XHTML (The XHTML)
<div id="live-preview-form" class="lp-block">
<p>
<strong>Your Name:</strong><br />
<input type="text" name="name" id="name" value="" class="input" /><br /><br />
<strong>Your Email:</strong><br />
<input type="text" name="email" id="email" value="" class="input" /><br /><br />
<strong>Your Website:</strong><br />
<input type="text" name="website" id="website" value="" class="input" /><br /><br />
<strong>Your Comment:</strong><br />
<textarea name="comment" id="comment" class="input" rows="10"></textarea>
</p>
</div>
<div id="live-preview-display" class="lp-block">
<div id="lp-avatar"></div>
<div id="lp-name"></div>
<div id="lp-comment"></div>
</div>
You can set up your XHTML any way you'd like. It's important for the sake of consistency to make your preview display look as closely as possible to your real comments display.
您可以根据需要设置XHTML。 为了保持一致性,重要的是使预览显示与实际评论显示尽可能接近。
CSS (The CSS)
.lp-block { width:400px; float:left; }
.lp-block input, .lp-block textarea { width:90%; }
#live-preview-display { background:#eee; padding:10px; margin-left:50px; margin-top:20px; }
#lp-name { font-weight:bold; }
#lp-avatar { float:right; margin:0 0 20px 20px; }
#lp-comment { padding-top:10px; font-style:italic; line-height:19px; }
Make your "preview" CSS look just like your real comments display.
使您的“预览” CSS看起来就像您的实际注释显示一样。
MooTools JavaScript (The MooTools JavaScript)
(function($){
window.addEvent('domready',function(){
//the build process
var build = function() {
//vars (fields) and blocks
var lpcomment = $('lp-comment'), lpname = $('lp-name'), lpavatar = $('lp-avatar');
var name = $('name'), email = $('email'), website = $('website'), comment = $('comment');
//comment
lpcomment.set('text',comment.value);
lpcomment.set('html',lpcomment.get('html').replace(/\n/g,'<br />'));
//name & websites
if(website.value && (website.value).test(/http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{2}/)) {
lpname.set('html','<a href="' + website.value + '">' + name.value + '</a> says:');
}
else {
lpname.set('text',name.value + ' says:');
}
//gravatar
if(email.value && (email.value).test(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/)) {
var md5Email = MD5(email.value);
lpavatar.set('html','<img src="http://www.gravatar.com/avatar.php?gravatar_id=' + md5Email + '&size=80&rating=G&default=http%3A%2F%2Fdavidwalsh.name%2Fwp-content%2Fthemes%2Fdavid-walsh%2Fgraphics%2Fcom.jpg" />');
}
};
//comment...easy
$$('#live-preview-form input, #live-preview-form textarea').addEvents({
keyup: build,
blur: build
});
});
})(document.id);
The JavaScript is quite easy. Note that I'm doing very basic validation and formatting -- you may get as fancy or simple as you'd like. Also note that I'm using an MD5 function for the gravatar functionality. The MD5 function was found here.
JavaScript非常简单。 请注意,我正在做非常基本的验证和格式化-您可能会喜欢上自己想像的或简单的。 另请注意,我使用的是MD5功能的Gravatar功能。 在这里找到 MD5功能。
This is as basic as it gets. You may want to implement functionality that checks for valid URLs and email addresses. You may also want to implement a regular expression that turns two line breaks into </p><p> tags. If you'd like to implement a more sophisticated system, I highly recommend using Thomas Aylott's SubtleTemplates.
这是最基本的。 您可能想要实现检查有效URL和电子邮件地址的功能。 您可能还想实现一个将两个换行符转换为</ p> <p>标记的正则表达式。 如果您想实现一个更复杂的系统,我强烈建议您使用Thomas Aylott的SubtleTemplates 。
mootools