.labels()

.labels()

.labels()Returns: jQuery

Description: Finds all label elements associated with the first selected element.

  • version added: 1.12.labels()

    • This method does not accept any arguments.

This can be used to find all the <label> elements associated with an <input> element. The association can be through nesting, where the label is an ancestor of the input, or through the for attribute on the label, pointing at the id attribute of the input. If no labels are associated with the given element, an empty jQuery object is returned.

This methods mimics the native labels property, which isn't supported in all browsers. In addition, this method also works for document fragments.

Example:

Highlight all labels of the input element

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>labels demo</title>
  <link rel="stylesheet" href="/doc_/-code-jquery-com-ui-1-12-0-themes-smoothness-jquery-ui-css.html?lang=en">
  <script src="//code.jquery.com/jquery-1.12.4.js" rel="external nofollow" ></script>
  <script src="//code.jquery.com/ui/1.12.0/jquery-ui.js" rel="external nofollow" ></script>
</head>
<body>
 
<label>
  Firstname
  <input id="name">
</label>
<label for="name">Please enter your name</label>
 
<script>
$( "input" ).labels().addClass( "ui-state-highlight" )
</script>
 
</body>
</html>

Demo:

© The jQuery Foundation and other contributors
Licensed under the MIT License.
https://api.jqueryui.com/labels

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部