[英]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.