在jQuery中,符号“*”有什么用途?

3
我正在阅读jQuery,但不知道为什么要使用("*"),请解释一下它的作用。
<script>
    $(document).ready(function(){
        $("button").click(function(){
            $("*").hide();
        });
    });
</script>

请参考Selectors stackoverflow文档 http://stackoverflow.com/documentation/jquery/389/selectors/4136/overview#t=2017040107020653041 在提问之前,请先参考文档。 - Lucky
你为什么在这个问题中标记了Java? - Krishnanunni P V
3个回答

2

* 是 jQuery 中的选择器,它可以选择所有元素,包括 html、head 和 body 等,没有任何条件限制。

下面是一个例子,说明它的用法。

$(document).ready(function(){
    $("button").click(function(){
        $("*").hide();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  Hello text
</div>
<button>Click Me to Hide everything</button>

* selector can be used with elements that it selects all child elements within the specified element. without any condition

$(document).ready(function(){
    $("button").click(function(){
        $("div *").toggle();
    });
});
div{
border:0.5px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <p>Hello text</p>
  <a>Here is a link </a>
</div>

<button>Click Me to Hide/ Show elements inside the div</button>


0

0

$(*) 是 jQuery 中的选择器,用于选择所有元素

因此

$("*").hide(); 会隐藏所有元素

$(document).ready(function(){
    $("button").click(function(){
        $("*").hide();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Hello</div>
<p>World</p>
<span>Good Morning</span>
<button>Hide</button>

文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接