获取修改元素属性

attr() 方法,可以获取修改元素属性。

获取属性,使用:attr("属性名")   相当于JavaScript中的getAttribute()。

设置属性,使用:attr("属性名","属性值")   相当于JavaScript中的setAttribute()。

删除属性,使用:removeAttr("属性名")    相当于JavaScript中的removeAttribute()。 

示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取修改元素属性</title>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.3.1.min.js"></script>
</head>
<body>
<a href="http://www.163.com">网易</a>
<script>
    alert($("[href]").attr('href'));
    $("a").attr("href","http://www.baidu.com");
    $("a").text("百度一下")
</script>
</body>
</html>

  

猜你喜欢

转载自www.cnblogs.com/max-hou/p/9131824.html