简体   繁体   English

自定义jQuery选择框

[英]Customizing jQuery selectbox

Can I customize this plugin so that on clicking any other place in document except selectbox - its close? 我可以自定义此插件,以便在单击文档中除selectbox以外的其他任何位置时关闭它吗? I try to add click event for body tag, but it seems dont work.. 我尝试为body标签添加click事件,但似乎不起作用。

html html

<select>
    <option value="1">1</option>
    <option value="2">2</option>
</select>

css 的CSS

.selectbox {
    width: 100px;
    border: 1px solid #000;
    padding: 2px 5px;
}
.list {
    background-color: #eee;
    border: 1px #ddd solid;
    padding: 0;
    position: absolute;
    list-style: none;
    width: 100%;
}
.list li:hover {
    color: #fff;
    background-color: #00f;
}

js js

! function(t) {
return t.fn.selectbox = function(e) {
    var i;
    return i = t.extend({
        customList: !1
    }, e), this.each(function() {
        var e, n, s, c;
        return s = t(this).css({
            opacity: 0,
            position: "absolute",
            "z-index": 2,
            top: 0,
            width: "100%",
            height: "100%"
        }), i.customList && (n = s.children()), c = s.wrap("<div class='selectbox'></div>").parent().css({
            position: "relative"
        }), c.append("<span class='value'>" + s.val() + "</span>"), i.customList && (s.hide(), e = c.append("<ul class='list'></ul>").find("ul"), n.each(function() {
            return e.append("<li>" + this.text + "</li>")
        }), e.hide()), s.on("change", function() {
            return c.children(".value").text(t(this).val()), !1
        }), i.customList && (c.click(function() {
            return e.toggle(), !1
        }), e.children().click(function() {
            return e.toggle(), s.val(n.eq(e.children().index(this)).attr("value")).trigger("change"), !1
        })), t(this)
    })
}, !1
}(jQuery);

and with this code hook up plugin 并与此代码挂钩插件

$('select').selectbox({customList: true});

Here is JsFiddle DEMO 这是JsFiddle演示

You can hide the options like this, bind click on document and check if the selectbox is clicked or not, then hide. 您可以隐藏这样的选项,绑定单击文档并检查是否单击了选择框,然后隐藏。

$(document).on('click',function(e){
    if(!$(e.target).is('select')){
        $('.selectbox ul').hide();
    }
});

DEMO 演示

You can listen to the document on clicks and just hide the .selectbox ul tags. 您可以单击单击来收听document ,而只是隐藏.selectbox ul标签。 Since the <select> naturally eats the click event to stop it bubbling up to the document, you don't need to worry about checking where the event's come from - you know it's not come from the selectbox. 由于<select>自然会吃掉click事件以阻止它冒泡到文档中,因此您不必担心检查事件的来源-您知道它不是来自selectbox。

Generally you should be very leery of binding event handlers to the document, but since this shouldn't ever get called too often (click) is a relatively infrequent event), you should be fine. 通常,您应该非常谨慎地将事件处理程序绑定到文档,但是由于永远不要调用此事件(单击)是相对少见的事件),所以应该没事。

This should work: 这应该工作:

(function($) {
    $(document).on('click', function(e) {
        $('.selectbox').find('ul').hide();
    });
})(jQuery);

Here's an example jsFiddle 这是一个jsFiddle示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM