私はファイル入力(画像)を持っていますが、width
またはそのheight
の内容に応じてdiv
のclass
を設定します。アップロードされた画像の幅と高さを比較する
私のコードは動作しません(最後のif
文だけ)、div
のクラスはアップロードされた画像に関係なく変更されません。
これが私のHTMLです:
<div class="row">
<div class="col-md-12">
<div id="blah" class="profilePic">
<img src="profilkep.jpg" />
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12">
<input type="file" onchange="previewFile()">
</div>
</div>
そして、これは私のJavaScriptのである:
function previewFile(){
var preview = document.querySelector('img'); //selects the query named img
var file = document.querySelector('input[type=file]').files[0]; //sames as here
var reader = new FileReader();
reader.onloadend = function() {
preview.src = reader.result;
}
if (file) {
reader.readAsDataURL(file); //reads the data as a URL
} else {
preview.src = "";
}
if (file.width < file.height) {
document.getElementById("blah").setAttribute("class", "blahblah");
} else {
document.getElementById("blah").setAttribute("class", "profilePic");
}
}
ありがとう、良い点、私は2番目を削除しましたが、まだ動作していません。 – user6725906
未定義とはどういう意味ですか? – user6725906
今私の答えを見て、それは働いている。あなたが彼に依頼したときにfile.widthは定義されていません –