jQuery中focusin()和focus()、find()和children()的差别

2021-07-15 02:06

阅读:705

jQuery中focus()和focusin()、focus()和children()的差别

focus()和focusin()

focus()和focusin()的差别在于focusin()支持事件的冒泡。以下举例说明:


html lang="en">
head>
  meta charset="utf-8">
  title>focusin demotitle>
  style>
  span {
    display: none;
  }
  style>
  script src="https://code.jquery.com/jquery-1.10.2.js">script>
head>
body>

p>input type="text"> span>focusin firespan>p>
p>input type="password"> span>focusin firespan>p>

script>
$( "p" ).focusin(function() {
  $( this ).find( "span" ).css( "display", "inline" ).fadeOut( 1000 );
});
script>

body>
html>

当我们点击输入框时。其获得焦点。导致获得焦点事件向上冒泡,使得p标签触发获得焦点事件,而focus()并不支持事件冒泡,相同地。focusout()支持事件冒泡,而blur()不支持。

find()和children()

find()和children()的差别在于find()向下追溯多级子节点,而children()仅仅向下追溯一级子节点。find()和children()相同的地方是他们在寻找子节点时都不包括自身节点。


html lang="en">
head>
    meta charset="UTF-8">
    title>Documenttitle>
    style type="text/css">
        p{
            font-size: 20px;
            width: 200px;
            color: blue;
            font-weight: bold;
            margin: 0 10px;
          }
          .hilite {
            background: yellow;
          }
    #test{
        font-weight: bolder;
    }
    style>
head>
body>
ul class="level-1">
  li class="item-i">Ili>
  li class="item-ii">II
    ul class="level-2">
      li class="item-a">Ali>
      li class="item-b">B
        ul class="level-3">
          li class="item-1">1li>
          li class="item-2">2li>
          li class="item-3">3li>
        ul>
      li>
      li class="item-c">Cli>
    ul>
  li>
  li class="item-iii">IIIli>
ul>
    script src="jquery-2.1.4.js">script>
    script>
        $( "li.item-ii" ).find( "li" ).css( "background-color", "red" );
    script>

body>
html>

倘若将上例中的find()替换为children(),会得到不同的结果。


评论


亲,登录后才可以留言!