2017-02-14 9 views
2

私はFileUploadを実装しており、より良い見た目にするためにブートストラップボタンクラスを使って<div>を作成しました。button-div内のどこでも入力フィールドをクリック可能にする

私はこれを行って以来、特定の位置( 'aufnehmen'の上)でinputをクリックするだけです。

divのどこかをクリックすると、ウィンドウを開く方法を教えてもらえますか?

ここに私の問題を示すフィドルがあります。

.btn-lg{ 
 
    font-size: 36px !important; 
 
} 
 

 
.fileUpload { 
 
    position: relative; 
 
    overflow: hidden; 
 
    margin: 10px; 
 
} 
 

 
.fileUpload input.upload { 
 
    position: absolute; 
 
    top: 0; 
 
    right: 0; 
 
    margin: 0; 
 
    padding: 0; 
 
    font-size: 20px; 
 
    cursor: pointer; 
 
    opacity: 0; 
 
    filter: alpha(opacity=0); 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 

 

 
<div class="fileUpload btn btn-primary btn-lg"> 
 
     <span>Foto aufnehmen</span> 
 
     <input type="file" accept="image/*" name="sampleFile" id="cameraImg" class="upload" onchange="processImg(this)" 
 
       value=""> 
 
    </div>

答えて

3

あなたはファイル入力をラップするlabelの代わりdivを使用することができます。

<label for="cameraImg"> 

.btn-lg { 
 
    font-size: 36px !important; 
 
} 
 
.fileUpload { 
 
    position: relative; 
 
    overflow: hidden; 
 
    margin: 10px; 
 
} 
 
.fileUpload input.upload { 
 
    position: absolute; 
 
    top: 0; 
 
    right: 0; 
 
    margin: 0; 
 
    padding: 0; 
 
    font-size: 20px; 
 
    cursor: pointer; 
 
    opacity: 0; 
 
    filter: alpha(opacity=0); 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 

 

 
<label for="cameraImg" class="fileUpload btn btn-primary btn-lg"> 
 
    <span>Foto aufnehmen</span> 
 
    <input type="file" accept="image/*" name="sampleFile" id="cameraImg" class="upload" onchange="processImg(this)" value=""> 
 
</label>

+0

また、いいソリューションですが、私は@ RoryMcCrossanから1つを取り出しました。なぜなら、ラベルでdivを変更しなくて済むからです。とにかくありがとう! – BayLife

+0

'labels'に' inputs'をラップすることは、とにかく幸せなコーディングです。 –

+0

ああ、私はあなたが言ったことをgoogledし、それは正しいようです。それから私は良い練習のために行くだろう!これを指摘してくれてありがとう。 – BayLife

0

あなたが追加することができます。

.fileUpload input.upload { 
    width: 100%; 
    height: 100%; 
} 
1

をあなたはinputheight: 100%を設定することにより、この問題を解決することができます

.btn-lg { 
 
    font-size: 36px !important; 
 
} 
 
.fileUpload { 
 
    position: relative; 
 
    overflow: hidden; 
 
    margin: 10px; 
 
} 
 
.fileUpload input.upload { 
 
    position: absolute; 
 
    top: 0; 
 
    right: 0; 
 
    margin: 0; 
 
    padding: 0; 
 
    font-size: 20px; 
 
    cursor: pointer; 
 
    opacity: 0; 
 
    filter: alpha(opacity=0); 
 
    border: 1px solid #C00; 
 
    height: 100%; /* add this rule */ 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 

 
<div class="fileUpload btn btn-primary btn-lg"> 
 
    <span>Foto aufnehmen</span> 
 
    <input type="file" accept="image/*" name="sampleFile" id="cameraImg" class="upload" onchange="processImg(this)" value=""> 
 
</div>
を0

+0

私はちょうど1つのルールを追加しなければならなかったので、あなたを選ぶことに決めました。ありがとう! – BayLife

+0

問題ありません。お返事を受け入れることを忘れないでください –

0

セットwidth:100%; height:100%;それが動作input.uploadのために...試してみてください、以下のチェック

.btn-lg{ 
 
    font-size: 36px !important; 
 
} 
 

 
.fileUpload { 
 
    position: relative; 
 
    overflow: hidden; 
 
    margin: 10px; 
 
} 
 

 
.fileUpload input.upload { 
 
    width:100%; 
 
    height:100%; 
 
    position: absolute; 
 
    top: 0; 
 
    right: 0; 
 
    margin: 0; 
 
    padding: 0; 
 
    font-size: 20px; 
 
    cursor: pointer; 
 
    opacity: 0; 
 
    filter: alpha(opacity=0); 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 

 

 
<div class="fileUpload btn btn-primary btn-lg"> 
 
     <span>Foto aufnehmen</span> 
 
     <input type="file" accept="image/*" name="sampleFile" id="cameraImg" class="upload" onchange="processImg(this)" 
 
       value=""> 
 
    </div>

0

スニペットこの

.btn-lg{ 
 
    font-size: 36px !important; 
 
} 
 

 
.fileUpload { 
 
    position: relative; 
 
    overflow: hidden; 
 
    margin: 10px; 
 
} 
 

 
.fileUpload input.upload { 
 
    position: absolute; 
 
    top: 0; 
 
    right: 0; 
 
    margin: 0; 
 
    padding: 0; 
 
    font-size: 20px; 
 
    cursor: pointer; 
 
    opacity: 0; 
 
    filter: alpha(opacity=0); 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 

 

 

 
<div class="fileUpload btn btn-primary btn-lg" > 
 
Foto aufnehmen 
 
<input type="file" style="opacity: 0.0; position: absolute; top:0; left: 0; bottom: 0; right:0; width: 100%; height:100%;" /> 
 
</div>

2

あなたはすぐにあなたの問題に答えの束を得ましたしかし、私はそれがどのような問題であるかをどのようにして見つけ出したかを示すことも重要だと思います。そのような問題を一般的にデバッグする方法です。

デベロッパーコンソールの要素を調べ、そのコードの上にカーソルを置くと、アウトラインが強調表示されます。

enter image description here

そのように、あなたが入力要素自体がボタンほど高くないことがわかりますし、その領域の外のクリックは、入力のクリックイベントをトリガしなかった理由です。

その時点から、あなた自身の解決策を提示することができました(いくつかの回答としてheight: 100%を設定するか、top: 0; right: 0; bottom: 0; left: 0など)。

+0

ありがとうございます。未来のために私はこれを心に残していきます! – BayLife

+0

すばらしい説明。私はこれを2回upvoteすることができれば願っています;) – BayLife

関連する問題