query

Module

Method

dom.query()
static Array<HTMLElement> query (selector [,context=document])
获取符合选择器的所有元素.
Parameters:
  • selector (string|HTMLCollection|Array<HTMLElement>) – 字符串表示 css3 选择器
  • context (string|HTMLCollection|Array<HTMLElement>) –

    选择器参考上下文,.

    context 限制同 selector 相同.

Returns:

符合选择器字符串的 dom 节点数组

注意 context 的取值

<!DOCTYPE html>
<html>
<head>
    <style>
        button { margin:10px; }
    </style>
    <script src="//g.alicdn.com/kissy/k/1.4.7/seed.js" data-config="{combine:true}"></script>
</head>
<body>
    
    <div class='outer' id='s'>
        <div class='item'>
            item1
        </div>
    </div>
    
    <div class='outer'>
        <div class='item'>
            item2
        </div>
    </div>
    
    <div class='item'>
        item3
    </div>
    
    <button>{"selector":".item"}</button>
    <button>{"selector":".item","context":".outer"}</button>
    <button>{"selector":".item","context":"#s"}</button>
    
<script>
    KISSY.use('core',function (S) {
        var $=S.all;
        $(".item",$(".outer")).css("color","red");
        $("button").on("click",function(){
            $("div").css("color","");
            var el=$(this);
            var content=S.JSON.parse(el.html());
            $(content.selector,content.context).css("color","red");
        });
        
    });
</script>
</body>
</html>