如何从 jQuery 选择器获取 DOM 元素?

jqueryweb developmentfront end technology

要从 jQuery 选择器获取 DOM 元素,请使用 $('#element').get(0)。这将返回 DOM 元素。

示例

您可以尝试运行以下代码来了解如何从 jQuery 选择器获取 DOM 元素。对于我们的复选框示例,请按以下代码获取 DOM 元素:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $(":checkbox").click(function() {
     if ($(this).is(":checked")) {
       alert("Checkbox checked!");
     }
  });
});
</script>
</head>
<body>
  <input type="checkbox" id="demo" />
</body>
</html>

相关文章