http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 14841|回复: 6

[JavaScript] js实现多个输入框同时输入的效果

[复制链接]
发表于 2012-10-31 18:09:20 | 显示全部楼层 |阅读模式
先看看实现后的效果吧,
2011110813162058.gif
这是一个Cookie数据生成的列表,
每次单击查询会存储一个域名,并把最后一次查询的域名放在最上方。本例子最多存储10个,大家可以根据自己情况进行设置
下在咱们一起来看看是怎么实现的吧、
先写一个操作Cookie的JS文件如下
[HTML] 纯文本查看 复制代码
/**
* Cookie plugin
*
* Copyright (c) [url=http://sufei.cnblogs.com]http://sufei.cnblogs.com[/url]
*
*/
jQuery.cookie = function (name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


有了这个文件之后我们来给要使用这Cookie的Text做一下处理吧,
代码如下
[HTML] 纯文本查看 复制代码
function Init() {
    $("#allSitesBoxHdl")[0].style.display = 'none';//不显示Cookie的列表
    $(":text").each(function () {//给所有的text加属性
        $(this).bind("keyup", OnKeyup); //按键时
        $(this).bind("mousedown", BoxShowUrls); //鼠标安下时
        $(this).bind("mouseout", BoxHide); //鼠标离开时
        $(this).bind("focus", closeIME); //处到焦点时
        $(this).bind("paste", OnPaste); //处理http;//
        $(this)[0].setAttribute('autocomplete', 'off');
    });
    //取出当前Cookie
    var icpSite = $.cookie("site");
    if (icpSite) {//如果Cookie存在的话取第一个值给当前框
        icpSite = icpSite.split('|')[0];
        $("#site").val(icpSite);
    }
}

这里面所用到的方法如下,具体的意思我就不一个一个的解释了,如果需要说明的请大家留言,我一定回复
[HTML] 纯文本查看 复制代码
function getid(id) {
    return (typeof id == 'string') ? document.getElementById(id) : id
};
function getOffsetTop(el, p) {
    var _t = el.offsetTop;
    while (el = el.offsetParent) {
        if (el == p) break;
        _t += el.offsetTop
    }
    return _t
};
function getOffsetLeft(el, p) {
    var _l = el.offsetLeft;
    while (el = el.offsetParent) {
        if (el == p) break;
        _l += el.offsetLeft
    }
    return _l
};

var currentInput = null;

function BoxShow(e) {
    var input = e;
    if (!input.id) {
        input = e.target ? e.target : e.srcElement;
    }
    currentInput = input;
    FillUrls("site");
    var box = getid("allSitesBoxHdl");
    if (box.style.display == 'block' && currentInput.id == input.id) {
        return;
    }
    box.style.left = (getOffsetLeft(input)) + 'px';
    box.style.top = (getOffsetTop(input) + (input.offsetHeight - 1)) + 'px';
    box.style.width = (input.offsetWidth - 4) + 'px';
    box.style.display = 'block';
}
function BoxShowUrls(e) {
    BoxShow(e);
}
function InputSetValue(val) {
    var obj = currentInput;
    obj.value = val;
    if (obj.getAttribute('url') == 'true') {
        var tags = document.getElementsByTagName('input');
        for (var i = 0; i < tags.length; i++) {
            if (tags.getAttribute('url') == 'true' && tags != obj) {
                tags.value = val;
            }
        }
    }
    BoxHide();
}
//删除时使用,传入一个要删除的值就可以删除
function DelAllSitesValue(value) {
    var allSites = $.cookie("site");
    allSites = allSites.replace(value + "|", "");
    $.cookie("site", allSites, { expires: 7 });
    FillUrls("site");
}
function BoxHide() {
    if (getid("allSitesBoxHdl")) {
        getid("allSitesBoxHdl").style.display = 'none';
    }
}
//加载列表
function FillUrls(cookieName) {
    var urls = $.cookie(cookieName);
    var html = "";
    if (urls) {
        var urllist = urls.split('|');
        var forlength = 0;
        var stringcookie;
        for (var i = urllist.length - 1; i >= 0; i--) {
            var textval = urllist;
            if ($.trim(textval) != "" && $.trim(textval) != "undefined") {
                html += "<li class=\"lis\"><a href=\"javascript:InputSetValue('" + textval + "');\">" + textval + "</a></li><br/>";
                forlength = forlength + 1;
                if (forlength > 10) {
                    $.cookie("site", stringcookie, { expires: 7 });
                    break;
                } else {
                    stringcookie = textval + "|" + stringcookie;
                }
            }
        }
    } else {
        html += "<li>没有记录</li>"
    }
    getid("allSitesBoxContent").innerHTML = html;
}
function closeIME(e) {
    var obj = e.target ? e.target : e.srcElement;
    obj.style.imeMode = 'disabled';
}

function OnPaste(e) {
    var obj = e.target ? e.target : e.srcElement;
    setTimeout("MoveHttp('" + obj.id + "')", 100);
}
function MoveHttp(id) {
    var val = getid(id).value;
    val = val.replace("http://", "");
    if (val[val.length - 1] == '/') {
        val = val.substring(0, val.length - 1);
    }
    getid(id).value = val;
}
function OnKeyup(e) {
    var obj = e.target ? e.target : e.srcElement;
    setTimeout("addInput('" + obj.id + "')", 200);
}
function addInput(id) {
    var obj = getid(id);
    //如果是一个没有True的input不执行
    if (obj.getAttribute('url') == 'true') {
        if (obj.value.indexOf('。') > 0) {
            obj.value = obj.value.replace('。', '.');
        }
        var tags = document.getElementsByTagName('input');
        for (var i = 0; i < tags.length; i++) {
            if (tags.getAttribute('url') == 'true' && tags != obj) {
                tags.value = obj.value;
            }
        }
    }
}

function Init() {
    $("#allSitesBoxHdl")[0].style.display = 'none';
    $(":text").each(function () {
        $(this).bind("keyup", OnKeyup);
        $(this).bind("mousedown", BoxShowUrls);
        $(this).bind("mouseout", BoxHide);
        $(this).bind("focus", closeIME);
        $(this).bind("paste", OnPaste);
        $(this).bind("mouseout", BoxHide);
        $(this)[0].setAttribute('autocomplete', 'off');
    });

    //取出Cookie
    var icpSite = $.cookie("site");
    if (icpSite) {
        //取出Cookie不为空的话就给当前框
        icpSite = icpSite.split('|')[0];
        $("#site").val(icpSite);
    }
}

在这里面还附带了这样一个效果,就是同时输入多个输入框的值,

如果那个输入框要使用这样的效果只要添加一个属性为url="true"就行了,这样方便 可操作性强,想给那个框加效果就加上这个属性,不想加的直接不加url="true"
就OK了。
在使用这个效果的界面添加如下代码
[HTML] 纯文本查看 复制代码
 <div style="display: none; position: absolute;" id="allSitesBoxHdl" class="classlist"
           >
            <ul id="allSitesBoxContent">
            </ul>
        </div>
   <script type="text/javascript">            Init();</script>

除此之外的JS直接放在一个Js文件里,引用进来就行了
下拉列表是怎么加载的呢?看下面的一个方法就知道了
[HTML] 纯文本查看 复制代码
//加载列表
function FillUrls(cookieName) {
    var urls = $.cookie(cookieName);
    var html = "";
    if (urls) {
        var urllist = urls.split('|');
        var forlength = 0;
        var stringcookie;
        for (var i = urllist.length - 1; i >= 0; i--) {
            var textval = urllist;
            if ($.trim(textval) != "" && $.trim(textval) != "undefined") {
                html += "<li class=\"lis\"><a href=\"javascript:InputSetValue('" + textval + "');\">" + textval + "</a></li><br/>";
                forlength = forlength + 1;
                if (forlength > 10) {//在这里我只加载10条,大家可以根据自己的情况进行调整
                    $.cookie("site", stringcookie, { expires: 7 });
                    break;
                } else {//如果超出10个的话就取最后10个
                    stringcookie = textval + "|" + stringcookie;
                }
            }
        }
    } else {
        html += "<li>没有记录</li>"
    }
    getid("allSitesBoxContent").innerHTML = html;
}

完成了这些之后我们只需要在单击查询时进行存储Cookie就行了,看下面的方法
[HTML] 纯文本查看 复制代码
//操作Cookie类
function setCookie(name, value) {

    var oldcookie = $.cookie(name);
    if (oldcookie == null) {
        $.cookie(name, value, { expires: 7 });
    } else {
        if ($.cookie(name).indexOf(value) == -1) {
            $.cookie(name, oldcookie + "|" + value, { expires: 7 });
        } else {
            $.cookie(name, oldcookie.replace(value, "") + "|" + value, { expires: 7 });
        }
    }
    FillUrls(name);
}

调用 时这样写
setCookie("site", strdomin);
好了功能完成了
进行具体的测试
代码写的不是很好,希望大家多提提建议,我们进行相应修改争取更完善。
Cookie是存储的客户端的,一个并且只能访问同域名下的Cookie,子域名之间可以相互访问,只要加上
domain属性就行了,存储的方法如下
$.cookie("domain", value, { expires: 7, domain: "baidu.com" });
取的时间直接写 $.cookie("domain");就好了,只要是子域名,都这样调用,这样可以达到本域名下的Cookie共享的功能。
Cookie的有效利用会给我们的网站带来N多意想不到的效果和功能,大家交流下。希望多提提建议

源码下载:
baiduLis百度关键字下拉框实现.zip (140.74 KB, 下载次数: 0)


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-11-11 13:44:09 | 显示全部楼层
看过帖子回复一下是个好习惯
发表于 2014-11-13 15:58:35 | 显示全部楼层
膜拜中....!
回复

使用道具 举报

发表于 2014-11-13 15:58:53 | 显示全部楼层
膜拜中....!
回复

使用道具 举报

发表于 2014-11-17 16:16:30 | 显示全部楼层
膜拜中.....
回复

使用道具 举报

发表于 2015-4-22 11:13:00 | 显示全部楼层
感谢您的无私奉献,真是帮了我的大忙了
发表于 2015-4-23 14:37:58 | 显示全部楼层
其实我是来找JS点赞的代码的!好奇进来而已!受教了!
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-23 19:17

© 2014-2021

快速回复 返回顶部 返回列表