2016-08-03 76 views
0

我有一个下拉按钮,当进入网页时,按钮显示“选择区域”。如果你将鼠标悬停在该按钮上,你会看到不同的区域。单击下拉菜单中的属性时更改按钮的文字?

按钮的主要价值需要改变,当他们点击他们想要的地区。

此外,我希望按钮位于右侧的搜索栏内,但它总是将它放在下面,就像您在示例中看到的一样。我尝试了很多不同的选项,但我无法修复这个问题。

html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td { 
 
    margin: 0; 
 
    padding: 0; 
 
    border: 0; 
 
    font-size: 100%; 
 
    font-family: 'Helvetica Neue', Helvetica, Arial, Sans-Serif; 
 
    vertical-align: baseline; 
 
    outline: none; 
 
} 
 

 
body { 
 
    background: url(../images/background/body_background.png) no-repeat center center fixed; 
 
    -webkit-background-size: cover; 
 
    -moz-background-size: cover; 
 
    -o-background-size: cover; 
 
    background-size: cover; 
 
} 
 

 
.logo img{ 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    display: block; 
 
    margin-top: 50px; 
 
    margin-bottom: 50px; 
 
} 
 

 
.SearchSummoners { 
 
    margin: auto; 
 
    width: 35%; 
 
    padding: 10px; 
 
    background-color: rgba(0, 0, 0, 0.7); 
 
    border: 1px solid; 
 

 
    -moz-border-image: -moz-linear-gradient(top, #006184 0%, #303142 100%); 
 
    -webkit-border-image: -webkit-linear-gradient(top, #006184 0%, #303142 100%); 
 
    border-image: linear-gradient(to bottom, #006184 0%, #303142 100%); 
 
    border-image-slice: 1; 
 
} 
 

 
/* Dropdown Button */ 
 
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
/* The container <div> - needed to position the dropdown content */ 
 
.region_dropdown_section { 
 
    position: relative; 
 
    display: inline-block; 
 
} 
 

 
/* Dropdown Content (Hidden by Default) */ 
 
.region_dropdown_content { 
 
    display: none; 
 
    position: absolute; 
 
    background-color: #f9f9f9; 
 
    min-width: 160px; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
} 
 

 
/* Links inside the dropdown */ 
 
.region_dropdown_content a { 
 
    color: black; 
 
    padding: 12px 16px; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 

 
/* Change color of dropdown links on hover */ 
 
.region_dropdown_content a:hover {background-color: #f1f1f1} 
 

 
/* Show the dropdown menu on hover */ 
 
.region_dropdown_section:hover .region_dropdown_content { 
 
    display: block; 
 
} 
 

 
/* Change the background color of the dropdown button when the dropdown content is shown */ 
 
.region_dropdown_section:hover .dropbtn { 
 
    background-color: #3e8e41; 
 
} 
 

 
.Searchbox_Summoners { 
 
    margin: auto; 
 
    display: block; 
 
    width: 65%; 
 
} 
 

 
#SearchBox{ 
 
\t \t margin-right: 10%; 
 
\t \t margin-left: 10%; 
 
\t \t width: 80%; 
 
\t \t background-color: white; 
 
\t \t height: 40px;; 
 
\t } 
 

 
#SearchInput{ 
 
\t \t width: 70%; 
 
\t \t line-height: 40px; 
 
\t \t background: white; 
 
\t \t border: 0; 
 
\t \t outline: 0; 
 
\t \t margin: 0; 
 
\t \t padding: 0; 
 
\t \t margin-left: 20px; 
 
    font-size: 24px; 
 
\t }
<?php define('DeniedAccessFiles', TRUE); ?> 
 

 
<?php include 'header.php'; ?> 
 
<div class="logo"> 
 
    <img src="https://placeholdit.imgix.net/~text?txtsize=47&txt=500%C3%97225&w=500&h=225"> 
 
</div> 
 
    <div class="SearchSummoners"> 
 
    <div id="SearchBox"> 
 
     <form method="POST"> 
 
     <input id="SearchInput" value="Enter the Summoner Name" onfocus="if(this.value == 'Enter the Summoner Name') { this.value = ''; } " onblur="if(this.value == '') { this.value = 'Enter the Summoner Name'; }" type="text" name="SummonerName"></input> 
 
     </form> 
 
    </div> 
 
    <div class="region_dropdown_section"> 
 
     <button class="dropbtn">Select Region</button> 
 
     <div class="region_dropdown_content"> 
 
     <a href="#">North America</a> 
 
     <a href="#">Europe West</a> 
 
     <a href="#">Europe NE</a> 
 
     <a href="#">Korea</a> 
 
     </div> 
 
    </div> 
 
    </div> 
 
<?php include 'footer.php'; ?>

+0

你需要在JavaScript或解决方案jquery? –

+0

那么我使用@Ivan Karaman的解决方案。但现在有一个bug。 每当我点击一个区域时,它会改变,但当我点击按钮外部时,区域会被删除,而按钮只会变成空白。 – BPrepper

回答

2

添加到链接点击触发和改变的onclick按钮上的文字...

更新:修正了空按钮上的文字

$(document).ready(function(){ 
 
    var regionDropDown = $('.region_dropdown_section'), 
 
     regionButton = regionDropDown.find('button'), 
 
     regionList = regionDropDown.find('.region_dropdown_content').children(); 
 
    
 
    $(regionList).on('click', function(e){ 
 
    var region = e.target; 
 
    regionButton.text(region.text).val(region.text); 
 
    }); 
 
});
html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td { 
 
    margin: 0; 
 
    padding: 0; 
 
    border: 0; 
 
    font-size: 100%; 
 
    font-family: 'Helvetica Neue', Helvetica, Arial, Sans-Serif; 
 
    vertical-align: baseline; 
 
    outline: none; 
 
} 
 

 
body { 
 
    background: url(../images/background/body_background.png) no-repeat center center fixed; 
 
    -webkit-background-size: cover; 
 
    -moz-background-size: cover; 
 
    -o-background-size: cover; 
 
    background-size: cover; 
 
} 
 

 
.logo img{ 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    display: block; 
 
    margin-top: 50px; 
 
    margin-bottom: 50px; 
 
} 
 

 
.SearchSummoners { 
 
    margin: auto; 
 
    width: 55%; 
 
    padding: 10px; 
 
    background-color: rgba(0, 0, 0, 0.7); 
 
    border: 1px solid; 
 

 
    -moz-border-image: -moz-linear-gradient(top, #006184 0%, #303142 100%); 
 
    -webkit-border-image: -webkit-linear-gradient(top, #006184 0%, #303142 100%); 
 
    border-image: linear-gradient(to bottom, #006184 0%, #303142 100%); 
 
    border-image-slice: 1; 
 
} 
 

 
/* Dropdown Button */ 
 
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
/* The container <div> - needed to position the dropdown content */ 
 
.region_dropdown_section { 
 
    display: inline-block; 
 
    width: calc(20% - 10px); 
 
} 
 

 
/* Dropdown Content (Hidden by Default) */ 
 
.region_dropdown_content { 
 
    display: none; 
 
    position: absolute; 
 
    background-color: #f9f9f9; 
 
    min-width: 160px; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
} 
 

 
/* Links inside the dropdown */ 
 
.region_dropdown_content a { 
 
    color: black; 
 
    padding: 12px 16px; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 

 
/* Change color of dropdown links on hover */ 
 
.region_dropdown_content a:hover {background-color: #f1f1f1} 
 

 
/* Show the dropdown menu on hover */ 
 
.region_dropdown_section:hover .region_dropdown_content { 
 
    display: block; 
 
} 
 

 
/* Change the background color of the dropdown button when the dropdown content is shown */ 
 
.region_dropdown_section:hover .dropbtn { 
 
    background-color: #3e8e41; 
 
} 
 

 
.Searchbox_Summoners { 
 
    margin: auto; 
 
    display: block; 
 
    width: 65%; 
 
} 
 

 
#SearchBox{ 
 
    display: inline-block; 
 
\t \t margin-right: 10%; 
 
\t \t margin-left: 10%; 
 
\t \t width: 50%; 
 
\t \t background-color: white; 
 
\t \t height: 40px;; 
 
\t } 
 

 
#SearchInput{ 
 
\t \t width: 70%; 
 
\t \t line-height: 40px; 
 
\t \t background: white; 
 
\t \t border: 0; 
 
\t \t outline: 0; 
 
\t \t margin: 0; 
 
\t \t padding: 0; 
 
\t \t margin-left: 20px; 
 
    font-size: 24px; 
 
\t }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<?php define('DeniedAccessFiles', TRUE); ?> 
 

 
<?php include 'header.php'; ?> 
 
<div class="logo"> 
 
    <img src="https://placeholdit.imgix.net/~text?txtsize=47&txt=500%C3%97225&w=500&h=225"> 
 
</div> 
 
    <div class="SearchSummoners"> 
 
    <div id="SearchBox"> 
 
     <form method="POST"> 
 
     <input id="SearchInput" value="Enter the Summoner Name" onfocus="if(this.value == 'Enter the Summoner Name') { this.value = ''; } " onblur="if(this.value == '') { this.value = 'Enter the Summoner Name'; }" type="text" name="SummonerName"></input> 
 
     </form> 
 
    </div> 
 
    <div class="region_dropdown_section"> 
 
     <button class="dropbtn">Select Region</button> 
 
     <div class="region_dropdown_content"> 
 
     <a href="#">North America</a> 
 
     <a href="#">Europe West</a> 
 
     <a href="#">Europe NE</a> 
 
     <a href="#">Korea</a> 
 
     </div> 
 
    </div> 
 
    </div> 
 
<?php include 'footer.php'; ?>

+0

你好 这对我来说已经很棒了。现在我有2个更多的问题。 单击某个区域并离开后,有时该按钮变为空白并且空了。此外,按钮的大小不断变化,是否有可能具有1个标准尺寸,因此在选择文字较小的区域时不会改变尺寸。 那么我会如何着手将右下角的搜索框中的下拉菜单?我试过这个,但那不起作用。 – BPrepper

1

一个解决办法是:

使用类region_dropdown_content附加Click事件处理程序在div下的每个锚。在这个处理程序内部根据用户选择更改字段的值。

该解决方案可以在jQuery以及javascript中实现。

的片段:

$(function() { 
 
    $('div.region_dropdown_content a').on('click', function(e) { 
 
    //$('#SearchInput').val(this.textContent); 
 
    }); 
 
}); 
 

 

 
// in javascript 
 

 
window.onload = function() { 
 
    document.querySelectorAll('div.region_dropdown_content a').forEach(function(ele, index) { 
 
    ele.addEventListener('click', function(e) { 
 
     document.getElementById('SearchInput').value = this.textContent; 
 
    }); 
 
    }) 
 
};
html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td { 
 
    margin: 0; 
 
    padding: 0; 
 
    border: 0; 
 
    font-size: 100%; 
 
    font-family: 'Helvetica Neue', Helvetica, Arial, Sans-Serif; 
 
    vertical-align: baseline; 
 
    outline: none; 
 
} 
 

 
body { 
 
    background: url(../images/background/body_background.png) no-repeat center center fixed; 
 
    -webkit-background-size: cover; 
 
    -moz-background-size: cover; 
 
    -o-background-size: cover; 
 
    background-size: cover; 
 
} 
 

 
.logo img{ 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    display: block; 
 
    margin-top: 50px; 
 
    margin-bottom: 50px; 
 
} 
 

 
.SearchSummoners { 
 
    margin: auto; 
 
    width: 35%; 
 
    padding: 10px; 
 
    background-color: rgba(0, 0, 0, 0.7); 
 
    border: 1px solid; 
 

 
    -moz-border-image: -moz-linear-gradient(top, #006184 0%, #303142 100%); 
 
    -webkit-border-image: -webkit-linear-gradient(top, #006184 0%, #303142 100%); 
 
    border-image: linear-gradient(to bottom, #006184 0%, #303142 100%); 
 
    border-image-slice: 1; 
 
} 
 

 
/* Dropdown Button */ 
 
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
/* The container <div> - needed to position the dropdown content */ 
 
.region_dropdown_section { 
 
    position: relative; 
 
    display: inline-block; 
 
} 
 

 
/* Dropdown Content (Hidden by Default) */ 
 
.region_dropdown_content { 
 
    display: none; 
 
    position: absolute; 
 
    background-color: #f9f9f9; 
 
    min-width: 160px; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
} 
 

 
/* Links inside the dropdown */ 
 
.region_dropdown_content a { 
 
    color: black; 
 
    padding: 12px 16px; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 

 
/* Change color of dropdown links on hover */ 
 
.region_dropdown_content a:hover {background-color: #f1f1f1} 
 

 
/* Show the dropdown menu on hover */ 
 
.region_dropdown_section:hover .region_dropdown_content { 
 
    display: block; 
 
} 
 

 
/* Change the background color of the dropdown button when the dropdown content is shown */ 
 
.region_dropdown_section:hover .dropbtn { 
 
    background-color: #3e8e41; 
 
} 
 

 
.Searchbox_Summoners { 
 
    margin: auto; 
 
    display: block; 
 
    width: 65%; 
 
} 
 

 
#SearchBox{ 
 
    margin-right: 10%; 
 
    margin-left: 10%; 
 
    width: 80%; 
 
    background-color: white; 
 
    height: 40px;; 
 
} 
 

 
#SearchInput{ 
 
    width: 70%; 
 
    line-height: 40px; 
 
    background: white; 
 
    border: 0; 
 
    outline: 0; 
 
    margin: 0; 
 
    padding: 0; 
 
    margin-left: 20px; 
 
    font-size: 24px; 
 
}
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> 
 

 
<div class="logo"> 
 
    <img src="https://placeholdit.imgix.net/~text?txtsize=47&txt=500%C3%97225&w=500&h=225"> 
 
</div> 
 
<div class="SearchSummoners"> 
 
    <div id="SearchBox"> 
 
     <form method="POST"> 
 
      <input id="SearchInput" value="Enter the Summoner Name" onfocus="if(this.value == 'Enter the Summoner Name') { this.value = ''; } " onblur="if(this.value == '') { this.value = 'Enter the Summoner Name'; }" type="text" name="SummonerName"></input> 
 
     </form> 
 
    </div> 
 
    <div class="region_dropdown_section"> 
 
     <button class="dropbtn">Select Region</button> 
 
     <div class="region_dropdown_content"> 
 
      <a href="#">North America</a> 
 
      <a href="#">Europe West</a> 
 
      <a href="#">Europe NE</a> 
 
      <a href="#">Korea</a> 
 
     </div> 
 
    </div> 
 
</div>

+0

啊,它工作的很好,但按钮必须改变文本。该地区不必进入搜索框。 此外,按钮需要在这样的右侧搜索框: https://gyazo.com/8d2f642b02b7a0de0ebfa5ebf11dd74c 感谢您的工作!对不起,如果我写错了,我的英文不好 – BPrepper

+0

@BPrepper不要担心。非常感谢与我们一起。 – gaetanoM

0
$('body').on('click',".region_dropdown_content a", function() 
{ 
var text = $(this).text(); 
$(".dropbtn").text(""); 
    $(".dropbtn").text(text); 
    $('#SearchInput').val(text); 
}); 
0

如果您正在使用jQuery

你可以给你一个锚类。

<div class="region_dropdown_section"> 
     <button class="dropbtn">Select Region</button> 
     <div class="region_dropdown_content"> 
     <a class="region" href="#">North America</a> 
     <a class="region" href="#">Europe West</a> 
     <a class="region" href="#">Europe NE</a> 
     <a class="region" href="#">Korea</a> 
     </div> 
    </div> 

如果其中之一被点击。将按钮的文本更改为锚标记的值。

$('.region').click(function(){ 
    $('.dropbtn').html(this.text); 
});