jQuery粗略源码解析5 属性操作

2020-02-24  本文已影响0人  波拉拉

1 HTML属性操作

<div dir="rtl">哈哈</div>
<script>
    $("div").attr({id:"bolala",alt:"test"})
        .removeAttr("dir");
</script>

2 DOM属性操作

具有 true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr()。

<form action="">
    <input type="radio" id="inp1">
    <input type="text" id="inp2" disabled >
</form>
<script>
    $(":radio").prop({checked:"true"});
    $(":text").removeProp("disabled");
</script>

3 类样式属性操作

<input type="button" value="切换">
<span>哈哈</span>
<script>
    $(":button").click(function () {
        $("span").toggleClass("active")
    })
</script>

4 值属性操作

上一篇 下一篇

猜你喜欢

热点阅读