Ale*_*lex 2182 javascript jquery this jquery-selectors
我的布局类似于:
<div id="..."><img src="..."></div>
Run Code Online (Sandbox Code Playgroud)
并希望使用jQuery选择器来选择点击img
内的子项div
.
为了得到div
,我有这个选择器:
$(this)
Run Code Online (Sandbox Code Playgroud)
如何让孩子img
使用选择器?
Sim*_*mon 2820
jQuery构造函数接受第二个参数context
,该参数可用于覆盖选择的上下文.
jQuery("img", this);
Run Code Online (Sandbox Code Playgroud)
这与使用.find()
这样的相同:
jQuery(this).find("img");
Run Code Online (Sandbox Code Playgroud)
如果你想要的imgs 只是被点击元素的直接后代,你也可以使用.children()
:
jQuery(this).children("img");
Run Code Online (Sandbox Code Playgroud)
phi*_*ash 461
你也可以用
$(this).find('img');
Run Code Online (Sandbox Code Playgroud)
这将返回img
作为后代的所有sdiv
rak*_*ice 134
如果你需要得到第一个img
只是一个级别,你可以做到
$(this).children("img:first")
Run Code Online (Sandbox Code Playgroud)
Roc*_*vic 75
如果您的DIV标记后面紧跟IMG标记,您还可以使用:
$(this).next();
Run Code Online (Sandbox Code Playgroud)
Ray*_*tor 63
该直接孩子
$('> .child', this)
Run Code Online (Sandbox Code Playgroud)
Lal*_*rya 40
您可以在下面找到父div的所有img元素
$(this).find('img') or $(this).children('img')
Run Code Online (Sandbox Code Playgroud)
如果你想要特定的img元素,你可以像这样写
$(this).children('img:nth(n)')
// where n is the child place in parent list start from 0 onwards
Run Code Online (Sandbox Code Playgroud)
你的div只包含一个img元素.所以对于以下是正确的
$(this).find("img").attr("alt")
OR
$(this).children("img").attr("alt")
Run Code Online (Sandbox Code Playgroud)
但是如果你的div包含更多img元素,如下所示
<div class="mydiv">
<img src="test.png" alt="3">
<img src="test.png" alt="4">
</div>
Run Code Online (Sandbox Code Playgroud)
然后你不能使用上层代码来找到第二个img元素的alt值.所以你可以尝试这个:
$(this).find("img:last-child").attr("alt")
OR
$(this).children("img:last-child").attr("alt")
Run Code Online (Sandbox Code Playgroud)
此示例显示了如何在父对象中查找实际对象的一般概念.您可以使用类来区分子对象.这很简单有趣.即
<div class="mydiv">
<img class='first' src="test.png" alt="3">
<img class='second' src="test.png" alt="4">
</div>
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
$(this).find(".first").attr("alt")
Run Code Online (Sandbox Code Playgroud)
更具体的:
$(this).find("img.first").attr("alt")
Run Code Online (Sandbox Code Playgroud)
您可以使用上面代码中的查找或子项.有关更多信息,请访问儿童http://api.jquery.com/children/并查找http://api.jquery.com/find/.参见示例http://jsfiddle.net/lalitjs/Nx8a6/
Osk*_*kar 33
在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
Run Code Online (Sandbox Code Playgroud)
Max*_*xam 30
试试这段代码:
$(this).children()[0]
Run Code Online (Sandbox Code Playgroud)
小智 30
在不知道DIV的ID的情况下,我认为你可以选择这样的IMG:
$("#"+$(this).attr("id")+" img:first")
Run Code Online (Sandbox Code Playgroud)
小智 23
您可以使用以下任一方法:
1找():
$(this).find('img');
Run Code Online (Sandbox Code Playgroud)
2个孩子():
$(this).children('img');
Run Code Online (Sandbox Code Playgroud)
Thi*_*gan 20
jQuery each
是一个选项:
<div id="test">
<img src="testing.png"/>
<img src="testing1.png"/>
</div>
$('#test img').each(function(){
console.log($(this).attr('src'));
});
Run Code Online (Sandbox Code Playgroud)
Den*_*s R 15
您可以使用Child Selecor引用父级中可用的子元素.
$(' > img', this).attr("src");
Run Code Online (Sandbox Code Playgroud)
以下是如果您没有参考,$(this)
并且您希望img
在div
其他功能中提供参考.
$('#divid > img').attr("src");
Run Code Online (Sandbox Code Playgroud)
这是一个功能代码,你可以运行它(这是一个简单的演示).
单击DIV时,您可以从一些不同的方法获取图像,在这种情况下,"this"是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)'));
});
});
Run Code Online (Sandbox Code Playgroud)
.the_div{
background-color: yellow;
width: 100%;
height: 200px;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
您的中可能包含0到许多<img>
标签<div>
。
要查找元素,请使用.find()
。
为了确保代码安全,请使用.each()
。
在一起使用.find()
和.each()
可以防止<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);
});
});
Run Code Online (Sandbox Code Playgroud)
#mydiv {
text-align: center;
vertical-align: middle;
background-color: #000000;
cursor: pointer;
padding: 50px;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)