ฉันมีเค้าโครงคล้ายกับสิ่งนี้:
<div id="..."><img src="..."></div>
และต้องการใช้ตัวเลือก jQuery เพื่อเลือกลูกที่imgอยู่ภายในdivเมื่อคลิก
เพื่อให้ได้divฉันมีตัวเลือกนี้:
$(this)
ฉันจะให้เด็กimgใช้เครื่องมือเลือกได้อย่างไร?
ฉันมีเค้าโครงคล้ายกับสิ่งนี้:
<div id="..."><img src="..."></div>
และต้องการใช้ตัวเลือก jQuery เพื่อเลือกลูกที่imgอยู่ภายในdivเมื่อคลิก
เพื่อให้ได้divฉันมีตัวเลือกนี้:
$(this)
ฉันจะให้เด็กimgใช้เครื่องมือเลือกได้อย่างไร?
คำตอบ:
ตัวสร้าง jQuery ยอมรับพารามิเตอร์ที่ 2 ที่เรียกว่าcontextซึ่งสามารถใช้เพื่อแทนที่บริบทของการเลือก
jQuery("img", this);
ซึ่งเหมือนกับการใช้.find()สิ่งนี้:
jQuery(this).find("img");
หาก imgs ที่คุณต้องการเป็นเพียงผู้สืบทอดโดยตรงขององค์ประกอบที่คลิกคุณสามารถใช้.children():
jQuery(this).children("img");
คุณสามารถใช้
$(this).find('img');
ซึ่งจะคืนค่าimgs ทั้งหมดที่เป็นลูกหลานของdiv
$(this).children('img')จะดีกว่า เช่น<div><img src="..." /><div><img src="..." /></div></div>เนื่องจากผู้ใช้ต้องการค้นหา img ระดับที่ 1
หากคุณต้องการได้อันดับแรกimgที่ลงหนึ่งระดับคุณสามารถทำได้
$(this).children("img:first")
:firstตรงกับ " ลงหนึ่งระดับเท่านั้น " หรือตรงกับ"อันดับแรก" imgที่พบ
.children()เป็นที่มาของ "down one level" และมาจาก:first"first"
.find()แทน.children()
thisแล้วมีการอ้างอิงถึงที่<div>มีอยู่<img>แล้ว แต่ถ้าคุณมีหลายระดับของแท็กเพื่อสำรวจจากการอ้างอิงคุณมีแล้วคุณแน่นอนสามารถเขียนมากกว่าหนึ่งchildren()สายแน่นอน
หากแท็ก DIV ของคุณถูกตามด้วยแท็ก IMG ทันทีคุณสามารถใช้:
$(this).next();
โดยตรงเด็ก
$('> .child-class', this)
คุณสามารถค้นหาองค์ประกอบ img ทั้งหมดของ div div ตามด้านล่าง
$(this).find('img') or $(this).children('img')
หากคุณต้องการองค์ประกอบ img ที่เฉพาะเจาะจงคุณสามารถเขียนเช่นนี้
$(this).children('img:nth(n)')
// where n is the child place in parent list start from 0 onwards
div ของคุณมีองค์ประกอบ img หนึ่งรายการเท่านั้น ดังนั้นสำหรับด้านล่างนี้ถูกต้อง
$(this).find("img").attr("alt")
OR
$(this).children("img").attr("alt")
แต่ถ้า div ของคุณมีองค์ประกอบ img เพิ่มเติมเช่นด้านล่าง
<div class="mydiv">
<img src="test.png" alt="3">
<img src="test.png" alt="4">
</div>
จากนั้นคุณไม่สามารถใช้รหัสด้านบนเพื่อค้นหาค่า alt ขององค์ประกอบ img ที่สอง ดังนั้นคุณสามารถลองสิ่งนี้:
$(this).find("img:last-child").attr("alt")
OR
$(this).children("img:last-child").attr("alt")
ตัวอย่างนี้แสดงแนวคิดทั่วไปว่าคุณสามารถค้นหาวัตถุจริงภายในวัตถุหลักได้อย่างไร คุณสามารถใช้คลาสเพื่อแยกความแตกต่างระหว่างวัตถุลูกของคุณ นั่นง่ายและสนุก กล่าวคือ
<div class="mydiv">
<img class='first' src="test.png" alt="3">
<img class='second' src="test.png" alt="4">
</div>
คุณสามารถทำได้ดังนี้:
$(this).find(".first").attr("alt")
และเฉพาะเจาะจงมากขึ้นเป็น:
$(this).find("img.first").attr("alt")
คุณสามารถใช้การค้นหาหรือเด็ก ๆ เป็นรหัสข้างต้น สำหรับข้อมูลเพิ่มเติมเยี่ยมชมเด็กhttp://api.jquery.com/children/และค้นหาhttp://api.jquery.com/find/ ดูตัวอย่างhttp://jsfiddle.net/lalitjs/Nx8a6/
วิธีอ้างอิงเด็กใน jQuery ฉันสรุปใน jQuery ต่อไปนี้:
$(this).find("img"); // any img tag child or grandchild etc...
$(this).children("img"); //any img tag child that is direct descendant
$(this).find("img:first") //any img tag first child or first grandchild etc...
$(this).children("img:first") //the first img tag child that is direct descendant
$(this).children("img:nth-child(1)") //the img is first direct descendant child
$(this).next(); //the img is first direct descendant child
ฉันไม่รู้ว่าคุณสามารถเลือก IMG แบบนี้ได้โดยไม่ทราบ ID ของ DIV
$("#"+$(this).attr("id")+" img:first")
ลองรหัสนี้:
$(this).children()[0]
$($(this).children()[0])เพียงห่อมันด้วยวิธีการที่:
$(this:first-child)
$($(this).children()[x])ใช้งานหรือถ้าคุณต้องการเป็นคนแรกเพียง$(this).eq(x) $(this).first()
คุณสามารถใช้วิธีใดวิธีหนึ่งต่อไปนี้:
1 ค้นหา ():
$(this).find('img');
เด็ก 2 คน ():
$(this).children('img');
jQuery's eachเป็นทางเลือกหนึ่ง:
<div id="test">
<img src="testing.png"/>
<img src="testing1.png"/>
</div>
$('#test img').each(function(){
console.log($(this).attr('src'));
});
คุณสามารถใช้Child Selecorเพื่ออ้างอิงอิลิเมนต์ลูกที่มีอยู่ในพาเรนต์
$(' > img', this).attr("src");
และด้านล่างคือถ้าคุณไม่มีการอ้างอิง$(this)และคุณต้องการอ้างอิงที่imgมีอยู่ในdivจากฟังก์ชั่นอื่น ๆ
$('#divid > img').attr("src");
นี่คือรหัสการทำงานที่คุณสามารถเรียกใช้ได้ (เป็นการสาธิตอย่างง่าย)
เมื่อคุณคลิก DIV คุณจะได้ภาพจากวิธีการต่าง ๆ ในสถานการณ์นี้ "นี่" คือ DIV
$(document).ready(function() {
// When you click the DIV, you take it with "this"
$('#my_div').click(function() {
console.info('Initializing the tests..');
console.log('Method #1: '+$(this).children('img'));
console.log('Method #2: '+$(this).find('img'));
// Here, i'm selecting the first ocorrence of <IMG>
console.log('Method #3: '+$(this).find('img:eq(0)'));
});
});
.the_div{
background-color: yellow;
width: 100%;
height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="my_div" class="the_div">
<img src="...">
</div>
หวังว่ามันจะช่วย!
คุณอาจมี 0 ถึง<img>แท็กมากมายภายในของคุณ<div>แท็กภายในของคุณ
ในการค้นหาองค์ประกอบให้ใช้ .find()เพื่อหาองค์ประกอบใช้
.each()เพื่อให้ปลอดภัยรหัสของคุณใช้
การใช้.find()และ.each()ร่วมกันป้องกันข้อผิดพลาดในการอ้างอิง null ในกรณีของ<img>องค์ประกอบ0 ในขณะที่ยังช่วยให้การจัดการหลาย<img>องค์ประกอบ
// Set the click handler on your div
$("body").off("click", "#mydiv").on("click", "#mydiv", function() {
// Find the image using.find() and .each()
$(this).find("img").each(function() {
var img = this; // "this" is, now, scoped to the image element
// Do something with the image
$(this).animate({
width: ($(this).width() > 100 ? 100 : $(this).width() + 100) + "px"
}, 500);
});
});
#mydiv {
text-align: center;
vertical-align: middle;
background-color: #000000;
cursor: pointer;
padding: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="mydiv">
<img src="" width="100" height="100"/>
</div>
$("body").off("click", "#mydiv").on("click", "#mydiv", function() {
event.stopImmediatePropagation()เพื่อป้องกันไม่ให้เกิดสิ่งนั้นขึ้น
$(document).ready(function() {
// When you click the DIV, you take it with "this"
$('#my_div').click(function() {
console.info('Initializing the tests..');
console.log('Method #1: '+$(this).children('img'));
console.log('Method #2: '+$(this).find('img'));
// Here, i'm selecting the first ocorrence of <IMG>
console.log('Method #3: '+$(this).find('img:eq(0)'));
});
});
.the_div{
background-color: yellow;
width: 100%;
height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="my_div" class="the_div">
<img src="...">
</div>
คุณสามารถใช้
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
$(this).find('img');
</script>