[英]jquery check if img is inside div
// wasd $(document).ready(function(e){ var keys = {}; $(document).keydown(function(event){ keys[event.which] = true; }).keyup(function(event){ delete keys[event.which]; }); var $d = $("img"); function gameLoop() { if (keys[68]) { $d.css("left", function(i,oldVal) { return parseInt(oldVal) + 5 + "px"; }); } else if (keys[65]) { $d.css("left", function(i,oldVal) { return parseInt(oldVal) - 5 + "px"; }); } if (keys[83]) { $d.css("top", function(i,oldVal) { return parseInt(oldVal) + 5 + "px"; }); } else if (keys[87]) { $d.css("top", function(i,oldVal) { return parseInt(oldVal) - 5 + "px"; }); } setTimeout(gameLoop, 20); } gameLoop(); $(document).focus(); }); // arrows $(document).ready(function(e){ var keys = {}; $(document).keydown(function(event){ keys[event.which] = true; }).keyup(function(event){ delete keys[event.which]; }); var $d = $("img"); function gameLoop() { if (keys[37]) { $d.css("left", function(i,oldVal) { return parseInt(oldVal) - 5 + "px"; }); } else if (keys[39]) { $d.css("left", function(i,oldVal) { return parseInt(oldVal) + 5 + "px"; }); } if (keys[40]) { $d.css("top", function(i,oldVal) { return parseInt(oldVal) + 5 + "px"; }); } else if (keys[38]) { $d.css("top", function(i,oldVal) { return parseInt(oldVal) - 5 + "px"; }); } setTimeout(gameLoop, 20); } gameLoop(); $(document).focus(); }); $(document).ready(function(){ if($(".goal:has(img)")){ alert("yes"); } });
img { position : absolute; left: 0; top: 0; } .goal{ width: 10px; height: 10px; background-color: green; float: right; }
<!DOCTYPE html> <html> <head> <title>Super Mario!</title> <link rel='stylesheet' type='text/css' href='style.css'/> </head> <body> <img src="http://i1061.photobucket.com/albums/t480/ericqweinstein/mario.jpg"/> <div class="goal"></div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <script src="script.js"></script> </html>
我想要一個在Mario(img)在目標(div)內時彈出的警報框。 謝謝您的幫助。 不介意我的js代碼只是隨機的,我不知道它是否接近正確,謝謝! 編輯:div是右上角的綠點。
嘗試$(".goal").find("img").length > 0
$(document).ready(function() { if ($(".goal").find("img").length > 0) { alert("yes"); } else { alert( "no" ); } });
img { position: absolute; left: 0; top: 0; } .goal { width: 10px; height: 10px; background-color: green; float: right; }
<!DOCTYPE html> <html> <head> <title>Super Mario!</title> <link rel='stylesheet' type='text/css' href='style.css' /> </head> <body> <img src="http://i1061.photobucket.com/albums/t480/ericqweinstein/mario.jpg" /> <div class="goal"></div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <script src="script.js"></script> </html>
您的問題有2個案例。
div內部任何級別的圖像,孩子,也都是孩子。 在這種情況下,
if ($(".goal").find('img').length) {
// yes
}
如果您只想檢查孩子而不是孩子,可以嘗試
if($(.goal).find('> img').length) {
// yes
}
我想要一個在Mario(img)在目標(div)內時彈出的警報框。
看來,您不需要檢查元素img
是否在div
內。 您只需要檢查mario img
的坐標是否在目標 div
的范圍內即可。
最簡單的方法是使用element.getBoundingClientRect
來獲取DOMRect
對象,該對象將保留元素的大小和位置。 用它來確定你的馬里奧是否在目標范圍內。
創建一個函數,以在每次鍵盤導航發生時檢查位置。 像這樣:
function checkMario() {
var goalPost = $('.goal')[0].getBoundingClientRect();
var mario = $('#mario')[0].getBoundingClientRect();
if ((goalPost.left - mario.left) < 60) {
$('#result').text('yesssss');
} else {
$('#result').text('no yet');
}
}
示例片段:
在此示例中,我一直簡單地簡單計算出left
位置是否在div
的范圍內。 然后,您將需要進一步優化以檢查所有方面。
嘗試使用鍵盤的向左/向右鍵將馬里奧琴移至目標 。
$(document).ready(function(e) { var keys = {}; $(document).keydown(function(event) { keys[event.which] = true; }).keyup(function(event) { delete keys[event.which]; }); var $d = $("img"); function gameLoop() { if (keys[68]) { $d.css("left", function(i, oldVal) { return parseInt(oldVal) + 5 + "px"; }); } else if (keys[65]) { $d.css("left", function(i, oldVal) { return parseInt(oldVal) - 5 + "px"; }); } if (keys[83]) { $d.css("top", function(i, oldVal) { return parseInt(oldVal) + 5 + "px"; }); } else if (keys[87]) { $d.css("top", function(i, oldVal) { return parseInt(oldVal) - 5 + "px"; }); } setTimeout(gameLoop, 20); } gameLoop(); $(document).focus(); }); // arrows $(document).ready(function(e) { var keys = {}; $(document).keydown(function(event) { keys[event.which] = true; }).keyup(function(event) { delete keys[event.which]; }); var $d = $("img"); function gameLoop() { if (keys[37]) { $d.css("left", function(i, oldVal) { return parseInt(oldVal) - 5 + "px"; }); checkMario(); } else if (keys[39]) { $d.css("left", function(i, oldVal) { return parseInt(oldVal) + 5 + "px"; }); checkMario(); } if (keys[40]) { $d.css("top", function(i, oldVal) { return parseInt(oldVal) + 5 + "px"; }); checkMario(); } else if (keys[38]) { $d.css("top", function(i, oldVal) { return parseInt(oldVal) - 5 + "px"; }); checkMario(); } setTimeout(gameLoop, 20); } gameLoop(); $(document).focus(); }); function checkMario() { var goalPost = $('.goal')[0].getBoundingClientRect(); var mario = $('#mario')[0].getBoundingClientRect(); if ((goalPost.left - mario.left) < 60) { $('#result').text('yesssss'); } else { $('#result').text('no yet'); } }
img { position: absolute; left: 0; top: 0; width: 60px; } .goal { width: 10px; height: 10px; background-color: green; float: right; } p { margin-top: 64px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <img id='mario' src="http://i1061.photobucket.com/albums/t480/ericqweinstein/mario.jpg" /> <div class="goal"></div> <br/> <p id="result"></p>
更改選擇器並檢查結果的長度: $(".goal img").length
$(document).ready(function() { if ($(".goal img").length) { alert("yes"); } });
img { position: absolute; left: 0; top: 0; } .goal { width: 10px; height: 10px; background-color: green; float: right; }
<!DOCTYPE html> <html> <head> <title>Super Mario!</title> <link rel='stylesheet' type='text/css' href='style.css' /> </head> <body> <img src="http://i1061.photobucket.com/albums/t480/ericqweinstein/mario.jpg" /> <div class="goal"> <img> </div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <script src="script.js"></script> </html>
嘗試這個
$(document).ready(function(){
if($("img").closest('.goal').length){
alert("yes");
}
});
使用find
檢查圖像標簽的length
。
$(document).ready(function(){ if ($('.goal').find('img').length >0 ) alert("yes"); else alert("no"); });
img { position : absolute; left: 0; top: 0; } .goal{ width: 10px; height: 10px; background-color: green; float: right; }
<!DOCTYPE html> <html> <head> <title>Super Mario!</title> <link rel='stylesheet' type='text/css' href='style.css'/> </head> <body> <img src="http://i1061.photobucket.com/albums/t480/ericqweinstein/mario.jpg"/> <div class="goal"></div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <script src="script.js"></script> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.