jquery属性操作
属性操作
1.属性
设置单选框被选中:
$(document).ready(function(){
    $(":checkbox").attr("checked","true");
});
取消选中:
$(document).ready(function(){
    $(":checkbox").removeAttr("checked");
});
禁用和选中所有页面上的复选框:
$(document).ready(function(){
   $("input[type='checkbox']").prop("disabled", false);
   $("input[type='checkbox']").prop("checked", true); 
});
移除用prop选中的单选框:
$(document).ready(function(){
    $("input[type='checkbox']").removeProp("checked");
});
<body>
<div>test1</div>
<div id="d" >test2</div>
  <input type="button" value="Input Button"/>
  <input type="checkbox" />
  <input type="password"  />
  <input type="radio" />
  <input type="reset" />
 <input type="text" value="文本框" />
  <input type="submit" />
</body>
2.css类
  为p元素添加类名:
$(document).ready(function(){
    $("p").addClass("selected");
});
 删除p元素的类名:
$(document).ready(function(){
    $("p").removeClass("selected");
});
如果存在(不存在)就删除(添加)一个类
 $("p").toggleClass("selected");
<body>
  <p>p</p>
</body>
3.html属性 (html,text,val)
 html返回p元素内容:
$(document).ready(function(){
      var n= $("p").html();
      alert(n);
});
  html设置p元素内容(标签有效):
$(document).ready(function(){
    $("p").html("hello <b>world</b>");  
});
 text返回p元素内容:
$(document).ready(function(){
    var p= $("p").text();  
    alert(p);
});
  text设置p元素内容(只是纯文本):
$(document).ready(function(){
    $("p").text("Hello world!");  
});
 val设置input的值:
$(document).ready(function(){
    $("input").val("hello world!");
});
<body>
 <input type="text" value="文本框" />
  <p>p</p>
</body>
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号