mootools_使用MooTools进行评论预览

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

翻译自: https://davidwalsh.name/comment-preview

mootools

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值