What is the use of (“*”) in jquery

一曲冷凌霜 提交于 2021-01-27 13:41:23

问题


I am reading jQuery, i don't know why use ("*") please explain it's helpful

<script>
    $(document).ready(function(){
        $("button").click(function(){
            $("*").hide();
        });
    });
</script>

回答1:


* is a selector in jquery which select everything without any condition including html, head and body.

Here is an example explaining its usage.

$(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>



回答2:


$(*) is used to select all elements inside the document. Refer this: https://www.w3schools.com/jquery/sel_all.asp




回答3:


$(*) is a selector in jqueryto select all elements

and hence

$("*").hide(); will hide all elements

$(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>

Docs



来源:https://stackoverflow.com/questions/43153792/what-is-the-use-of-in-jquery

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