简体   繁体   English

如何干净地访问jquery对象的直接后代?

[英]How do I access direct descendants of a jquery object cleanly?

I have the following line in my js 我的js中有以下行

var description = jqref.children('.category_info').children('.description').children('p').html();

which works exactly how I want it, though to me it feels way too clunky which feels like I am overlooking a better selector method. 这完全符合我的要求,但是对我来说感觉太笨拙了,好像我正在忽略更好的选择器方法。

I'm fairly certain I ought to be using the > direct child symbol but can't seem to implement it in the normal way as I am already dealing with a jquery object. 我相当确定我应该使用>直接子符号,但是由于我已经在处理jquery对象,因此似乎无法以常规方式实现它。

Use find with the child selector . find子选择器一起使用 I learned something new; 我学到新东西; you can prepend the selector with a child selector to make sure that you only get children: 您可以在选择器之前添加子选择器,以确保仅获得子选择器:

var description = jqref.find('> .category_info > .description > p').html();

JSFiddle: http://jsfiddle.net/5SktL/ JSFiddle: http : //jsfiddle.net/5SktL/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从同一viewModel中的不同分支访问后代? - How do I access descendants from a different branch in the same viewModel? 如何通过选择器仅选择缓存的jQuery上下文的直接后代 - How to select only direct descendants of cached jQuery context by selector 直接后代只有jQuery的find() - Direct descendants only with jQuery's find() 如何直接访问显示为未定义但在控制台登录时正确显示的对象键? - How do I access object keys that show as undefined with direct reference to them but appear correctly when console logging? 我如何使用jQuery / JavaScript使用Treetable插件影响/选择所有后代 - How do I affect/select all descendants with Treetable plugin using jQuery / javascript 如何删除jQuery组的其他成员的后代元素? - How can I remove elements of a jQuery group that are descendants of other members? CSS选择器排除某些后代及其jQuery对象的后代? - css selector to exclude certain descendants and their descendants of jquery object? 如何在抛出未知错误时清楚地读取属性? - How do I cleanly read a property in an unknown error thrown? 如何在 Javascript 中干净地为某些东西侧身设置动画? - How do I cleanly animate something sideways in Javascript? 如何干净地循环遍历嵌套对象? - How do I cleanly loop through nested objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM