2016-06-27 8 views
0

私はすべての国のイメージを持っていますが、ビジュアルに言語の名前の前にイメージを追加するにはどうすればいいですか?基本的には、各選択肢に国旗が表示されます。あなたの助けてくれてありがとう、これは私ができるほど詳細です。この選択リストに画像を追加するにはどうすればよいですか? Javascript?

<body> 
 

 
    <header> 
 
    <div id="country-select"> 
 
     <form action="" method="get"> 
 
     <select id="locale" name="locale"> 
 
      <option value="none">Select A Language</option> 
 
      <option value="en_US">English(US)</option> 
 
      <option value="en_GB">English(UK)</option> 
 
      <option value="bg_BG">Bulgarian</option> 
 
      <option value="cs_CS">Czech</option> 
 
      <option value="da_DK">Danish</option> 
 
      <option value="de_DE">German</option> 
 
      <option value="ek_GR">Greek</option> 
 
      <option value="es_ES">Spanish</option> 
 
      <option value="et_ET">Estonian</option> 
 
      <option value="fi_FI">Finnish</option> 
 
      <option value="fr_FR">French</option> 
 
      <option value="hu_HU">Hungarian</option> 
 
      <option value="it_IT">Italian</option> 
 
      <option value="lt_LT">Lithuanian</option> 
 
      <option value="lv_LV">Latvian</option> 
 
      <option value="nl_NL">Dutch</option> 
 
      <option value="no_NO">Norwegian</option> 
 
      <option value="pl_PL">Polish</option> 
 
      <option value="pt_PT">Portugese</option> 
 
      <option value="ro_RO">Romanian</option> 
 
      <option value="sk_SK">Slovak</option> 
 
      <option value="sl_SL">Slovenian</option> 
 
      <option value="sv_SE">Swedish</option> 
 
     </select> 
 
     <input value="Select" type="submit" /> 
 
     </form> 
 
    </div> 
 
    </header> 
 
    <script> 
 

 

 
     //Main Function: creates the page dynamically & switches the url to selected country 
 
     function GetSelectedItem() { 
 
     var option = document.getElementById("locale").value; 
 
     } 
 
\t 
 
\t //Main Function: Split the string of the URL 
 
\t function getParameterByName(name, url) { 
 
\t \t //Retrieves website URL 
 
\t \t if (!url) url = window.location.href; 
 
\t \t //Replaces the 1st parameter with the 2nd parameter 
 
\t \t name = name.replace(/[\[\]]/g, "\\$&"); 
 
\t \t //Takes all the regular expressions and results is the value we need 
 
\t \t var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url); 
 
\t \t //If empty or none are in there return Null 
 
\t \t if (!results) return null; 
 
\t \t //If the URL name doesn't have anything in index 2 return empty stress 
 
\t \t if (!results[2]) return ''; 
 
\t \t //Return the value without the regular expressions. 
 
\t \t return decodeURIComponent(results[2].replace(/\+/g, " ")); 
 
\t } 
 
\t 
 
\t //Set variable to the splitted string (e.g. en_GB would be the value inside of the getParameterByName() 
 
\t var localeVariable = getParameterByName('locale'); 
 
\t //Make the drop down menu stay on the country selected 
 
\t var temp = localeVariable; 
 
\t //Determine with for loop to go through each country with the countring variable j 
 
\t for(var i, j = 0; i = locale.options[j]; j++){ 
 
\t //if value matches go in the if to make the value the selected index 
 
\t \t if(i.value == temp){ 
 
\t \t \t //set whatever j equaled to the selected index. 
 
\t \t \t locale.selectedIndex = j; 
 
\t \t \t break; //Break if value matches 
 
\t \t } 
 
\t } 
 
\t 
 
    </script> 
 
</body>

+1

この回答を参照してください:http://stackoverflow.com/questions/2965971/how-to-add-a-images-in-select-list –

+1

@MagnusBuvarpありがとう、試してみましたが、うまく動作しませんでした。 – Giorgio

答えて

1

ただ、各オプションのテキストの前にimgを追加します。これと同じように:

<option value="en_GB" style="background-image:url(englishflag.jpg);">English(UK)</option> 
+0

それはまったく動作しません。 – Giorgio

+0

Firefoxでしか動作しません!私はすべてのブラウザ、特にGoogle Chromeをサポートする必要があります。あなたの助けをありがとう – Giorgio

+1

ええ、クロスブラウザは、この方法を使用することは困難ですが、それを行うための唯一の方法です。そうでなければ、標準の 'select'以外の何かを使うことができます – AJenkins

0

私はあなたがbackground-imageといくつかのパディングを使用してoptionタグのスタイルを設定することができると思います。 A class属性は本当にあなたがその方法を解決する場合に役立ちます

+0

ありがとう私はそれを試してみましょう! – Giorgio

+0

私が確認した...ちょうど試してみました。それは動作します – fedeghe

+0

私はちょうどあなたの方法をgoogled、それはFirefoxだけでなく、すべてのブラウザで動作しますか? – Giorgio

関連する問題