jQuery链Chaining

With jQuery, actions/methods can be chained together.

Chaining allows us to run multiple jQuery methods (on the same element) in one statement.

The following example links css(), slideUp() and slideDown() together. The "p1" element will first turn red, then slide up, then slide down:

$(document).ready(function()
  {
  $("button").click(function(){
    $("#p1").css("color","red").slideUp(2000).slideDown(2000);
  });
});
</script>
</head>
<body>

<p id="p1">111</p>
<button>点我</button>

Tip: When linking, lines of code can become very long. However, jQuery syntax is not very strict; you can write it in the format you want, including line breaks and indentation.

jQuery will throw away the extra whitespace and execute the above line of code as one long line of code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325989351&siteId=291194637