单击下拉菜单上的项目后,如何将焦点切换到输入文本?

时间:2015-04-07 01:30:41

标签: javascript jquery html css

我的网站有一个下拉菜单和一个输入框。为了让用户感到舒适,我认为拥有它会很好,当用户点击下拉菜单中的一个选项时,鼠标光标会立即聚焦在输入框内,以便他们可以立即开始输入,而不是每次点击它。

如何实现这一目标?

这是我的Jsfiddle:http://jsfiddle.net/mlynn/jyrbepyz/3/

谢谢。

HTML

<section id="heady">
    <div style="text-align: left;padding:25px 70px;display:inline-block;float:left;"><b><a href="index.html">Site</b></a></p></div>

    <div style="text-align: right;padding:25px 70px;display:inline-block;float:right;">    
            <a href="index.html">Home</a> | 
            <a href="index.html">Generic</a> |
            <a href="index.html">Elements</a> |
            <a href="index.html">Sign Up</a>
    </div>
</section>
<section id="wrapper">
    <br><br>
    <img src="images/blacksquare.png" width="525" height="197"></img>

    <br><br><br>
    <div>
        <div style="vertical-align:top;display:inline-block;float:left;">        
            <ul class="navbar cf">
                <!-- <li><a href="#">item 2</a></li> -->
                <li style="width:200px;">
                    <a href="#" class="ActiveListItem">#</a>
                    <ul>
                        <li><a href="#">1</a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                        <li><a href="#">4</a></li>
                        <li><a href="#">5</a></li>
                        <li><a href="#">6</a></li>
                        <li><a href="#">7</a></li>
                    </ul>
                </li>
            </ul>
        </div>                                                               
        <div class="container lister" style="display:inline-block;float:left;vertical-align:top;padding:0px 0px 0px 10px;">
            <form action="">
                <input type="text" id="todo" placeholder="Enter a To-do and hit enter">
            </form>
            <br>
            <!-- <ul class="active">
                <li>Work <a href="">X</a></li>
                <li>Sleep <a href="">X</a></li>
                <li>Repeat <a href="">X</a></li>
            </ul> -->
        </div>
        <div class="container lister" style="display:inline-block;float:left;vertical-align:top;padding:0px 0px 0px 10px;">
            <ul class="active">
                <li>Work <a href="">X</a></li>
                <li>Sleep <a href="">X</a></li>
                <li>Repeat <a href="">X</a></li>
            </ul>
        </div>
    </div>             
    <div class="Category1">
        <!--list items that user assigned "1" from dropdown menu would be placed in this div-->
    </div>

    <div class="Category2">
    </div>
    <div class="Category3">
    </div>
    <div class="Category4">
    </div>
    <div class="Category5">
    </div>  
    <div class="Category6">
    </div>  
    <div class="Category7">
    </div> 
</section>

<section id="feety">
I believe I exist
</section>

CSS

/*adder*/

@import url(http://fonts.googleapis.com/css?family=Open+Sans:400, 300, 600);


 * {
    padding:0;
    margin:0;
}


html {
 background:teal;
}


body {
    /*background:url('https://snap-photos.s3.amazonaws.com/img-thumbs/960w/4657039731.jpg');*/
}


a {
    color: #D9D9D9;
    text-decoration: none;
}
a:active, a:hover {
    text-decoration: underline;
}



#heady {
    text-align: center;
    width:100%;
    height:75px;
    background-color:#222;                       /*Back Colors*/
    font-family: Tahoma;
    font-size: 16px;
    color:white;
    position:relative; 
}

#wrapper {
    text-align: center;
    width:1000px;
    height:1000px;
    margin-left:auto;
    margin-right:auto;
    background-color:teal;                       /*Back Colors*/
    font-family: Tahoma;
    font-size: 16px;
    position:relative; 
}


#feety {
    text-align: center;
    width:100%;
    height:100px;
    background-color:darkslateblue;                       /*Back Colors*/
    font-family: Tahoma;
    font-size: 16px;
    color:white;
    position:relative; 
}





.Category1 {
    background:blue;
}


.Category2 {
    background:green;
}

.Category3 {
    background:yellow;
}

.Category4 {
    background:orange;
}

.Category5 {
    background:purple;
}

.Category6 {
    background:gold;
}

.Category7 {
    background:maroon;
}

/* clearfix */
/**
 * For modern browsers
 * 1. The space content is one way to avoid an Opera bug when the
 *    contenteditable attribute is included anywhere else in the document.
 *    Otherwise it causes space to appear at the top and bottom of elements
 *    that are clearfixed.
 * 2. The use of `table` rather than `block` is only necessary if using
 *    `:before` to contain the top-margins of child elements.
 */
.cf:before,
.cf:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.cf:after {
    clear: both;
}

.cf {
   * zoom: 1;
}


ul.navbar {

  background:white; 
  border-style:solid;
  border-color:gray;
  border-width:1px;
  width: 200px;
  border-radius: 4px;

}


.ActiveListItem:after {
    content: "\25BC\00a0\00a0";                        /*carat and spaces*/
    float:right;
    font-weight:900;
    padding: 0px 0px;
    font-size:100%; 
    line-height:20px;             /*keeps carat in center of text*/
}




ul.navbar li a.ActiveListItem {
    background:white !important;
    color:black;
    border-style:solid;
    border-color:white;
    border-radius:4px;
    padding:3px 5px !important;
    font-weight:normal !important;
    margin-left:14px;/* got the activeitem centered with the list text this way*/
    margin-right:0px;

}

ul.navbar li {
    position: relative;
}

ul.navbar li a {
    display: block;
    color: white;
    padding:10px 5px;
    text-decoration:none;
    transition: all .2s ease-in;

}

ul.navbar li a:hover,
ul.navbar li:hover > a {
    background:#a6d0e1; /*Leaving for now, but keep in mind things bold slowly when you change this to gradient*/
    color: #333;
    font-weight:900;

}

    ul.navbar li ul {
        margin-top: 1px;
        position: absolute;
        background: #222;
        font-size: 14px;
        min-width: 200px;
        display: none;
        z-index: 99;
        box-shadow: inset 0 2px 3px rgba(0,0,0,.6),
        0 5px 10px rgba(0,0,0,.6);
    }

ol, ul { list-style: outside none none; }

.hidden { display: none; }







/*Lister*/



.container {
    width: 60%;
    margin: 0px auto;
}

form {  }

input,
ul {
    background: #eee;
    border-radius: 5px;
    width: 100%;
    box-sizing: border-box;
    font-family:"Tahoma";
}

input {
    padding: 10px 10px 10px 20px;
    border: 1px solid #ccc;
}

.lister ul {
    list-style: square inside;
    padding: 10px;
}

.active { border: 1px solid #ccc; }

.inactive { display: none; }

.lister li {
    padding: 10px;
    font-weight: 600;
    color: #34495e;
}

.lister li:nth-child(odd) {
    background: #dadfe1;
    border-radius: 5px;
}

.lister li > a {
    float: right;
    text-decoration: none;
    color: #22313f;
    font-weight: bold;
    transition: all .2s ease-in-out;
}

.lister li > a:hover {
    font-size: 110%;
    color: #c0392b;
}


.lister li:before {
    content: "#";                        /*carat and spaces*/
    float:left;
    font-weight:900;
    padding: 0px 0px;
    font-size:100%; 
    line-height:20px;             /*keeps carat in center of text*/
}

JS

// sub menus identification
$(function() {

    $('.navbar ul li a').click(function(){  
        $('.navbar > li:first-child > a').text($(this).text());
        $('.navbar > li > ul').addClass('hidden');
        $('.navbar li ul').slideToggle(100);
    });

    $('.navbar > li').mouseenter(function(){
        $(this).find('ul').removeClass('hidden');
    });

    $('.ActiveListItem').click(function(){        
        $('.navbar li ul').slideToggle(300);
    });    
});
    //newList
$(document).ready(function() {

    var ul = $('.lister ul'),
        input = $('input');

    input.focus();  

    $('form').submit(function () {
        if (input.val() !== '') {
            var inputVal = input.val(),
                activeNumber = $('.ActiveListItem').text();

            if (activeNumber == "1") {
                /*I guess the fantasy code goes here...?*/
            }

            ul.append('<li>' + activeNumber + ' ' +inputVal + '<a href="">X</a></li>');
            if (ul.hasClass('inactive')) {
                ul.removeClass('inactive')
                    .addClass('active');
            }
        };
        input.val('');
        return false;
    });

    ul.on('click', 'a', function (e) {
        e.preventDefault();
        $(this).parent().remove();

        if (ul.children().length == 0) {
            ul.removeClass('active')
                .addClass('inactive');
            input.focus();  
        }
    });
});

1 个答案:

答案 0 :(得分:1)

您正在使用var ul = $('.lister ul')

去查看HTML中的.lister和孩子ul

我的意思是你可能想要定位所需的 DROPDOWN UL anchors
使用右选择器:

$(".navbar.cf li ul li").on("click", "a", function(e){
    e.preventDefault();
    input.focus(); 
});

http://jsfiddle.net/jyrbepyz/5/