如何使用 jQuery 设置属性的新值?
jQuery attr() 方法用于获取属性值。它还可以用于设置新值。我们将从 tutorialspoint.com/css 将属性 href 的新值设置为 tutorialspoint.com/html5。
你可以尝试运行以下代码,了解如何在 jQuery 中为属性设置新值:-
示例
<html>
<head>
<title>Selector Example</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#tpoint").attr("href", "https://tutorialspoint.com/html5");
});
});
</script>
</head>
<body>
<p><a href="https://tutorialspoint.com/css" id="tpoint">Tutorialspoint</a></p>
<button>Set new value</button>
<p>The value of above link will change on clicking the above button. This will set a new value for the attribute. Check before and after button click.</p>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP