0

以下に示す埋め込みjsスクリプトを使用してselect要素を作成しています。ページに選択要素があることがわかりますが、ドロップダウンは空白です。デフォルトの選択も表示されません。CSSが機能していないと思う理由は、サイズがかなりずれているためです。私はjsからではなく静的選択を行いましたが、それははるかに大きくなっています。助言がありますか?

*更新*

select要素を追加しましたが、今は2つあります。選択肢があり、CSS の影響を受けないものと、CSS シートによって適切にフォーマットされた空白のものです。何を与える?

<script>
             function processCSV(file,parentNode)
             {
                var frag = document.createDocumentFragment()
                , lines = file.split('\n'), option;                 
                var intial_option = document.createElement("option");

                intial_option.setAttribute("value","");
                intial_option.setAttribute("disabled","disabled");
                intial_option.setAttribute("selected","selected");
                intial_option.innerHTML = "Please select a Plant";
                frag.appendChild(intial_option)

                for (var i = 0, len = lines.length; i < len; i++){
                    option = document.createElement("option");
                    option.setAttribute("value", lines[i]);
                    option.innerHTML = lines[i];                        
                    frag.appendChild(option);
                    }

                parentNode.appendChild(frag);
                                            menuholder.appendChild(parentNode);
             }

             var plant_select = document.createElement("select");  
             var datafile = '';
             var xmlhttp = new XMLHttpRequest();

             plant_select.setAttribute("class", "selectbox");   
             plant_select.setAttribute("id", "plant_select");



             xmlhttp.open("GET","http://localhost:8080/res/plants.csv",true);
             xmlhttp.send();
             xmlhttp.onreadystatechange = function()
             {
                if(xmlhttp.status==200 && xmlhttp.readyState==4)
                {
                    processCSV(xmlhttp.responseText, plant_select);
                }
             }
        </script>

対応する CSS ファイルのセクションを以下に示します。

body
 {
    padding: 0;
     margin: 0;
background-color:#d0e4fe;
font-size: 2em;
      font-family: monospace;
      font-weight: bold;
  }

.menu_container
{
   position: relative;
    margin: 0 auto;
 }
.menu_element
{ 
float: right;
width: 33%;
}
4

1 に答える 1

2

plant_select を dom に挿入する必要があると思います。

したがって、processCSV を実行する前に、次のようにします。

var body_elem=document.getElementsByTagName('body')[0];
body_elem.appendChild(plant_select);

メニューが必要な場所に応じて、最初の行(追加する要素)を変更します。ドキュメント要素の作成と挿入に関する情報については、https: //developer.mozilla.org/en-US/docs/Web/API/Node.appendChild を参照してください。insertBeforeも参照してください。

実際、ドキュメントのどこにオプションを配置しているのかわかりません。

また、特に IE を使用しているため、plant_select.setAttribute("class", "selectbox"); ではなく、これが役立つ場合があります。plant_select.setAttribute("id", "plant_select");

試す

     plant_select.className="selectbox";   
     plant_select.id="plant_select";

特に IE では、属性をプロパティにマッピングすることを不適切に選択するという問題がありました。この方法で ID とクラスを設定すると、setAttribute よりも信頼性が高くなります。

于 2013-10-08T15:59:35.113 に答える