JQuery API: :button Selector
Button Selector sẽ lựa chọn tất các phần tử button hoặc tất cả các phần tử input có type = button. Một selector khác cũng có chức năng tương tự như $(":button") là $( "button, input[type='button']" ). Chú ý : Bởi vì :button là một Jquery ...
Button Selector sẽ lựa chọn tất các phần tử button hoặc tất cả các phần tử input có type = button.
Một selector khác cũng có chức năng tương tự như $(":button") là $( "button, input[type='button']" ).
Chú ý: Bởi vì :button là một Jquery extension và không phải là một phần thiết lập của CSS, các truy vấn sử dụng :button sẽ không thể phát huy hiệu quả của phương thức querySelectorAll(). Để đạt được hiệu quả cao nhất khi sử dụng :button để chọn các phần tử, đầu tiên hãy chọn các phần tử bằng cách sử dụng các selector CSS thuần túy, sau đó sử dụng bộ lọc .filter(":button").
Cú pháp
jQuery( ":button" )
Ví dụ
Tìm kiếm tất cả các thẻ button hoặc thẻ input có type=button và đổi màu cho chúng:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>all demo</title>
<style>
textarea {
height: 35px;
}
div {
color: red;
}
fieldset {
margin: 0;
padding: 0;
border-awidth: 0;
}
.marked {
background-color: yellow;
border: 3px red solid;
}
</style>
<script src="https://code24h.com/cnd/js/jquery/jquery-3.2.1.min.js"></script>
</head>
<body>
<h1>Học lập trình miễn phí tại code24h.com</h1>
<fieldset>
<input type="button" value="Input Button">
<input type="checkbox">
<input type="file">
<p type="button">
<input type="hidden">
<input type="image">
<input type="password">
<input type="radio">
<input type="reset">
<input type="submit">
<input type="text">
<select>
<option>Option</option>
</select>
<textarea></textarea>
<button>Button</button>
</fieldset>
<button onclick="myFunction()">Click vào đây để xem kết quả</button>
<script>
function myFunction(){
var input = $( ":button" ).addClass( "marked" );
$( "div" ).text( "For this type jQuery found " + input.length + "." );
// Prevent the form from submitting
$( "form" ).submit(function( event ) {
event.preventDefault();
});
}
</script>
</body>
</html>Kết quả:
Tham khảo: jquery.com
Nguồn: code24h.com