美文网首页
jQuery粗略源码解析5 属性操作

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

作者: 波拉拉 | 来源:发表于2020-02-24 19:14 被阅读0次

1 HTML属性操作

  • .attr() 获取设置HTML属性值
  • .removeAttr() 移除html属性
<div dir="rtl">哈哈</div>
<script>
    $("div").attr({id:"bolala",alt:"test"})
        .removeAttr("dir");
</script>

2 DOM属性操作

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

  • prop() 获取设置DOM属性值
  • removeProp() 移除DOM属性值
<form action="">
    <input type="radio" id="inp1">
    <input type="text" id="inp2" disabled >
</form>
<script>
    $(":radio").prop({checked:"true"});
    $(":text").removeProp("disabled");
</script>

3 类样式属性操作

  • addClass() 添加类名
  • removeClass() 移除类名
  • toggleClass() 切换类名
  • hasClass() 判断类名
<input type="button" value="切换">
<span>哈哈</span>
<script>
    $(":button").click(function () {
        $("span").toggleClass("active")
    })
</script>

4 值属性操作

  • val() 获取/修改当前的值

相关文章

网友评论

      本文标题:jQuery粗略源码解析5 属性操作

      本文链接:https://www.haomeiwen.com/subject/jiciqhtx.html