Child selector deprecated [duplicate]

筅森魡賤 提交于 2020-01-03 09:08:35

问题


Possible Duplicate:
What is the new proper way to use a child selector with a context node in jQuery?

From the jQuery docs:

Note: The $("> elem", context) selector will be deprecated in a future release. Its usage is thus discouraged in lieu of using alternative selectors.

http://api.jquery.com/child-selector/

What would be an alternative selector for this?


回答1:


$(context).children('elem')

also $("> elem", context) is deprecated, but $(context+" >elem") and $("parent>child") are not




回答2:


$('parent').children('childrenelements')

Is my guess :)

But as the other poster said, its only searching directly for childs in a context.




回答3:


For example, if the context is an element, you would use the selector for that element instead of specifying it as context. So instead of:

var main = $('#Main');
var mainDivs = $('> div', main);

you could use:

var mainDivs = $('#Main > div');



回答4:


I just came across the same issue, I was in the middle of a function and none of the above really helped me. .find seemed to do the trick. This is an example oif how it was useful:

$('selectedcontext').first(function(){
    // perform operations on selectedcontext for ex:
    if($(this).hasClass('someclass')){
        $(this).find('textarea').val();
    }
});


来源:https://stackoverflow.com/questions/7833558/child-selector-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!