jquery和js操作,【包含jquery掛事件】

來源:互聯網
上載者:User

標籤:參數   play   display   rem   tag   批量   對象   html   put   

JS
根據ID取元素,取到的是具體的元素
  var a = document.getElementById("p1");
根據CLASS取
  var a = document.getElementsByClassName("aa");
根據標籤名取
  var a = document.getElementsByTagName("div");
根據name取
  var a = document.getElementsByName("cc");
  alert(a); 

操作元素
操作內容
  非表單元素
    a.innerText = "hello";
    a.innerHTML = "<span style=‘color:red‘>world</span>";
  表單元素
    a.value = "hello";
  操作屬性
    a.setAttribute("bs","1");          bs為自己定義,兩個參數為設定 一個參數是擷取
    a.getAttribute("bs");
    a.removeAttribute("bs");
  操作樣式
    a.style.color = red;

  大量操作DIV隱藏
    var a = document.getElementsByClassName("aa");

        for(var i=0;i<a.length;i++)
        {
        a[i].style.display = "none";
        }


Jquery
  根據ID找元素,取到的是JQUERY對象
    var b = $("#a1");

  根據CLASS取
    var b = $(".aa");

  根據標籤名取
    var b = $("div");

  根據屬性篩選
    var b = $("[name=aa]");

  操作元素
  操作內容
  非表單元素
    b.text();
    b.html();
  表單元素
    b.val();
  操作屬性
  b.attr("bs","1");
  b.attr("bs");
  b.removeAttr("bs");
  操作樣式
  alert(b.css("color"));
  b.css("font-size","50px");

  alert(b[0]); 取jquery對象用.eq()   取元素本身用[]


  大量操作多個元素
  $(".aa").css("display","none");

 

 

 

 

jquery掛事件

 jquery可以加事件也可以掛事件,因為加上的事件改變比較麻煩,所以引入了掛事件,掛事件添加,改變相對簡單

  <input type="text" id="p1" />
  <input type="button" id="b1" value="掛事件" />
  <input type="button" id="b2" value="移除事件" />
  <script type="text/javascript"  Jquery加事件

  頁面載入完成
  $(document).ready(function(e) {

  給a1加點擊
  $("#a1").click(function(){
  alert(‘aa‘);
  })
  給class為aa的所有元素加事件
  $(".aa").click(function(){
  alert($(this).attr("bs"));
  $(".aa").css("background-color","#39F");
  $(this).css("background-color","red");
  })

  掛事件
  $("#b1").click(function(){

    $("#a1").bind("click",function(){

    alert("我是掛上的事件");

    });

  })
  $("#b2").click(function(){

    $("#a1").unbind("click");
    })
  });
</script>

jquery和js操作,【包含jquery掛事件】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.