jQuery文本掩码

时间:2011-04-22 21:25:11

标签: jquery passwords masking

我正在尝试创建一个小的剪切,允许屏蔽文本,除非链接悬停在它上面。像这样:

<span class="mask">My Information</span>
<a href="#">View Info</a>

页面加载时,页面显示如下:

<span class="masked">••••••••••••••</span>
<a href="#">View Info</a>

基本上,子弹占用的空间与其替换的文本相同。然后,当用户单击“查看信息”链接时,掩码将恢复为信息。

<span class="mask">My Information</span>
<a href="#">View Info</a>

在一个页面上会有多个这样的实例。

有什么想法吗?我真的不知道从哪里开始..我正在考虑制作一堆密码字段,但认为它会变得混乱..

3 个答案:

答案 0 :(得分:1)

远非完美我敢肯定!

jsfiddle

(function($) {

    $.fn.toggleMask = function() {

        this.each(function() {
            if ($(this).hasClass('masked')) {
                $(this).text($(this).attr('origValue'));
                $(this).removeClass('masked');
                var link = $(this).next();
                link.text(link.text().replace(/Show/,'Hide'));
            }
            else {
                $(this).attr('origValue', $(this).text());
                $(this).text(new Array($(this).text().length).join('•'));
                $(this).addClass('masked');
                var link = $(this).next();                
                link.text(link.text().replace(/Hide/,'Show'));
            }
        });

    return this;
};
})(jQuery);

$(function() {
    $('.mask').toggleMask();
    $('a').click(function() {
        $(this).prev().toggleMask();
        return false;
    });
});

答案 1 :(得分:0)

使用数据()。在加载时,您可以将每个$('。mask')的内容放在数据中:

$('.mask').each(function(){
    var t = $(this)
        ,text = t.text();
    t.data('text', text);
    //replace text with stars
});

然后你可以将一个函数绑定到用文本替换星号的鼠标点击。

答案 2 :(得分:0)

我知道这可能与问题无关'但是这里是如何使用jquery显示和隐藏密码

  $(function(){
        $('#showPassword').change(function(){
        var passwordText = $('#password');
        if($(this).prop('checked') == true){
            passwordText.get(0).type = 'text';
        }else{
            passwordText.get(0).type = 'password';
        }
     });
 });

<强> HTML

    userName :<input type="text" id="strUserName" name="strUserName"/>
    password:<input type="password" id="password" name="password" />
    show Password<input type="checkbox"  id="showPassword"  />