我不是什么高手,不过试着写了一个jquery的插件,希望能给你提供点参考
/*!
* jQuery alert plugin: simple alert for debugging
* Examples and documentation at:
* version 0.20 (26-MAY-2009)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
/**
* alert() takes a single string argument: $('#myObj').alert("option")
*
* val : alert + this.val(); default : true
* html : alert + this.html(); default : false
* attr : alert + this.attr(attrvalue); default : null
* css : alert + this.css(cssvalue) ; default : null
* ech : alert each elements; default : false
*
* @name alert
* @type jQuery
* @param String options Options which control the alert content
* @cat Plugins/Alert
* @return jQuery
* @author sam (mailto:www4169175@163.com)
*/
;(function($){
$.fn.alert = function(options){
// Default options
var opt = $.extend({
val : true,
html : false,
attr : null,
css : null,
ech : false
}, options);
if(!opt.ech){
alertMessage($(this));
}else{
$(this).each(function(){
alertMessage($(this));
});
}
function alertMessage(obj){
//create String
var alertString = "";
var br = "\n";
if(opt.val){
thisval = "value: " + obj.val();
alertString = alertString + thisval + br;
}
if(opt.html){
thisval = "html: " + obj.html();
alertString = alertString + thisval + br;
}
if(opt.attr != null){
thisval = "attr_"+opt.attr+": " + obj.attr(opt.attr);
alertString = alertString + thisval + br;
}
if(opt.css != null){
thisval = "css_"+opt.css+": " + obj.css(opt.css);
alertString = alertString + thisval + br;
}
//alert!
alert(alertString);
}
return this;
}
})(jQuery);