Created
March 7, 2012 12:24
-
-
Save kaixiang-li/1992816 to your computer and use it in GitHub Desktop.
微博添加话题组件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.topic = function(options) { | |
var opts = $.extend({}, $.fn.topic.defaults, options); | |
return this.each(function() { | |
var $this = $(this),rangeLength = opts.text.length + 1; | |
$(opts.btn).click(function(){ | |
$this.focus().val("#"+opts.text+"#"); | |
if($.browser.msie){ | |
var range = $this[0].createTextRange(); | |
var sel = range.duplicate(); | |
range.move("character",1); | |
sel.moveStart("character", rangeLength); | |
sel.setEndPoint("EndToStart", range); | |
sel.select(); | |
}else{ | |
$this[0].selectionStart = 1; | |
$this[0].selectionEnd = rangeLength; | |
} | |
}); | |
}); | |
}; | |
// plugin defaults | |
$.fn.topic.defaults = { | |
text: '请输入你的话题', | |
btn:"#btn" | |
}; | |
})(jQuery); | |
//example below: | |
$("#post").topic({btn:"#btn",text:"随便说点设么吧"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment