[英]jquery - how can I select element after '.first()' using?
使用名为.first()
jquery函数后,如何选择html tag
?
的HTML
<html>
<head> </head>
<body>
<div>
<a>
<img id="#hello">
</a>
</div>
<div></div>
</body>
</html>
JS(jQuery)
var elemFirst = $(div).first();
var selectImg = elemFirst.$('img#hello');
// var selectImg = elemFirst.find('img'); <- It working but I want to select manual
selectImg.addClass('some-css');
尊重地
使用find()
elemFirst.find('img#hello');
这将使用提供的选择器搜索直接元素和嵌套元素。
或者,您可以
$('div:first #hello')
甚至只是
$('#hello')
因为您使用的ID应该是唯一的。
您可以使用find()
:
var selectImg = elemFirst.find('img#hello');
但是您具有图像的id
,因此,您可以执行以下操作:
$('img#hello')
因为id
是唯一的。
您可以使用$('child','parent')
选择器,
var elemFirst = $('div').first();
var selectImg = $('#hello',elemFirst); //or just $('img',elemFirst);
另外,您的ID有一个#
,应该只是id="hello"
。
如果您需要将此值保留在ID中,请使用
var selectImg = $('img[id="#hello"]',elemFirst);
就像在html中一样,id在html中是唯一的,因此可以写
$("#\\#hello").addClass('some-css');
要么
var selectImg = elemFirst.find('img[id="#hello"]');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.