jQuery trigger() Method

https://www.w3schools.com/jquery/event_trigger.asp

Phương thức trigger () kích hoạt sự kiện đã chỉ định và hành vi mặc định của sự kiện (như gửi biểu mẫu) cho các phần tử được chọn.

Ví dụ 1:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("input").select(function(){
    $("input").after(" Text marked!");
  });
  $("button").click(function(){
    $("input").trigger("select");
  });
});
</script>
</head>
<body>

<input type="text" value="Hello World"><br><br>

<button>Trigger the select event for the input field</button>

</body>
</html>

Ví dụ 2:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script type="text/javascript" src="http://localhost/library/jquery/jquery.min.js"></script>
	<script type="text/javascript">
		jQuery(document).ready(function($) {
			$('input').click(function(event) {
				$(this).css('background', 'red');
			});
			$('button').click(function(event) {
				$('input').trigger('click');
			});
		});
	</script>
</head>
<body>
	<input type="text" name="trigger" placeholder="trigger">
	<button>Tricger</button>
</body>
</html>

Last updated

Navigation

Lionel

@Copyright 2023