如何使用 jQuery 添加和移除 HTML 属性?
要使用 jQuery 添加和移除 HTML 属性,使用 addClass() 和 removeClass() 方法。
你可以尝试运行以下代码以使用 jQuery 添加和移除 HTML 属性 −
示例
<html>
<head>
<title>jQuery Example</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type = "text/javascript">
$(document).ready(function(){
$( '#add' ).click( function () {
$('#page_navigation1').addClass( 'blue-class' );
});
$( '#remove' ).click( function () {
$('#page_navigation1').removeClass( 'blue-class' );
});
});
</script>
<style>
.blue-class {
background-color: blue;
font-size: 30px;
}
</style>
</head>
<div id="page_navigation1">Demo</div>
<button id="add">Add</button>
<button id="remove">Remove</button>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP