jQuery初学基础常用内容——操作元素属性

//在HTML中相关代码
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

<a id="a" asdf="123456" href="https://www.baidu.com">百度</a>

<script src="node_modules/jquery/dist/jquery.js"></script>
<script src="JS/main.js"></script>
</body>
</html>
//在JS中相关代码
$('#a').attr('asdf','654321');
$('#a').prop('asdf','654321');
//var t = $('#a').prop('text');//只传一个参数的时候,就是获取属性的值。
$('#a').removeAttr('asdf');
//attr显性   prop隐性
//有些属性attr 与 prop都可以修改,但是为什么有的prop就不可以修改?
// 因为在html的隐性的属性中,隐性的属性不一定就可以在标签中直接使用,比如我们自定义的属性
//prop方法就不可以修改其属性的值,但是attr可以。

猜你喜欢

转载自blog.csdn.net/qq527648162/article/details/84567517