Categories: Js/AJAX | Tags: , , | Views: 1,421

 

有些音标从键盘不好输入,需要一些辅助

 

调用:

1
$("#pronounce").yinbiao();

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 *    jquery.yinbiao 1.0 - 2009-09-28
 *
 *    All the new stuff written by pwwang (pwwang.com)    
 *    Feel free to do whatever you want with this file
 *
 */
 
(function($) {
    $.yinbiao = function(input, options) {
        var $input = $(input).attr("autocomplete", "off");
        var $results = $(document.createElement("ul"));    
        $results.addClass(options.resultsClass).appendTo('body');        
        resetPosition();
        $(window)
            .load(resetPosition)        // just in case user is changing size of page while loading
            .resize(resetPosition);
        $input.blur(function() {
            setTimeout(function() { $results.hide() }, 200);
        });
        $input.focus(function() {
            $input.setCaret(); 
            // need jquery.caretInsert, if no, comment it.
            yinbiao();
        });
 
        // help IE users if possible
        try {
            $results.bgiframe();
        } catch(e) { }
 
 
        function resetPosition() {
            var offset = $input.offset();
            $results.css({
                top: (offset.top + input.offsetHeight) + 'px',
                left: offset.left + 'px'
            });
        }
 
        function yinbiao() {
            var items = ['θ','æ','ŋ','ə','ð','ɔ','ʒ','ɛ','ʃ','ʌ'];
            displayItems(items);
        }
 
        function displayItems(items) {
            if (!items)    return;        
            if (!items.length) {
                $results.hide();
                return;
            }
            var html = '';
            for (var i = 0; i < items.length; i++)
                html += '<ul><li>' + items[i] + '</li></ul>';
            $results.html(html).show();
            $results
                .children('li')
                .mouseover(function() {
                    $results.children('li').removeClass(options.selectClass);
                    $(this).addClass(options.selectClass);
                })
                .click(function(e) {
                    e.preventDefault(); 
                    e.stopPropagation();
                    selectCurrentResult();
                });                
        }
 
        function getCurrentResult() {    
            if (!$results.is(':visible')) return false;
            var $currentResult = $results.children('li.' + options.selectClass);    
            if (!$currentResult.length)    $currentResult = false;        
            return $currentResult;
        }
 
        function selectCurrentResult() {
            $currentResult = getCurrentResult();
            if ($currentResult)     
                $input.insertAtCaret($currentResult.text());
                // need jquery.caretInsert, if no, user the following
                // $input.val($input.val()+$currentResult.text());
                // to add current result to the end of $input.
        }
    }
 
    $.fn.yinbiao = function(options){
        options = options || {};
        options.resultsClass = options.resultsClass || 'pwwang_com_results';
        options.selectClass = options.selectClass || 'pwwang_com_over';
        this.each(function() {
            new $.yinbiao(this, options);
        });
        return this;
    };
 
})(jQuery);

 

文件下载:

jquery.yinbiao.js

jquery.yinbiao.css

jquery.caretInsert.js

所需的插件(为了在光标处插入选中的音标),如果不用这个插件,则按照代码中给出的注释进行修改

 

这篇文章来自 迷途知返(PWWANG.COM), 转载请注明出处。 版权说明

  1. April 24th, 2010 at 01:03
    Reply | Quote | #1

    我正在做一个带数据库的网上英语电子词典项目,希望能开发出网民也能参与的wiki词典,但在电脑端或网页上通过输入法的特殊符号来输入音标的确很麻烦,看到博主的这个插件很好,但无奈对jQuery技术不是很熟,而这个又没有demo的,自己代码试写了几次都不行。不知道如何调用你的yinbiao jquery js函数。想请你讲清楚些。
    input text标签后带上 “$(“#pronounce”).yinbiao();”,这样调用为什么不行啊!
    希望博主能尽快给出具体的演示调用代码,不甚感激。如果太忙,麻烦您跟帖给予解答,如果能把演示代码发到我的Email上就更好了。

    • April 25th, 2010 at 16:05
      Quote | #2

      @raoyc2009:在引用了jquery库之后,应该这样调用
      $(function(){
      $(“#pronounce”).yinbiao(); // pronounce是你input box的id
      });

;) :| :x :twisted: :roll: :oops: :o :mrgreen: :lol: :idea: :evil: :cry: :arrow: :P :D :?: :? :) :( :!: 8O 8)

你可以使用@somebody:开头, 来邮件通知somebody你回复了他的留言(用户名区分大小写).