常用 jquery 程式碼片段 和 javascript 程式碼片段

來源:互聯網
上載者:User
1 .jquery篇


1.       Jquery擷取#first下 input名字叫price的元素的值

function getit(){       name = $("#first input[name='price']").val();       alert(name);}  alert($("#first input[name='price']").attr("value")); //擷取某個屬性的值

//$("ul li:last").html("22");  $("ul li:first").html("22"); 2.       Val()可以賦值也可以擷取值

alert($("#first input[name='price']").val());$("#first input[name='price']").val(123123123)

3.      each()遍曆select裡的option項

$("#sele option").each(function(){    if($(this).text()=="text"){    $(this).attr("selected","true");    }  });

 

4.      javascript的加法

parseInt(name)+parseInt(name2)

5.      頁面載入時運行

 $(document).ready(function(){            $('ul').css('color','red'); });


6. Jquery擷取元素的值的方法

$("#orderedlist").find("li") 等價於  $("#orderedlist li") Var  nname=$(this).parents('.list-comt').find('#nnuser').html();   //尋找某元素父元素 裡的 子項目的值

7.  在元素後追加

$("ul li").each(function(){  $(this).append("<font style='font-weight:bold;'>fireworf</font>");  }); //$(this).find("em").remove(); 刪除元素//$("a[href='/fyou']").html(); 擷取指定連結的超連結//$("a[href]")//有href屬性的超連結

8.  改變對象的CSS

$("#61dh a").css('color','#123456'); $('#sp').parent().css('background','#000');  //#sp的父原素的背景變黑

9.清除表單  表單複位

$("#reset").click(function(){ $("#form")[0].reset() } )


10.綁定事件

functionsecondClick() {    $("#dv1").unbind(); //解除對象事件    alert("second click!");}$("#dv1").bind("mouseover",secondClick); //$("#dv1").trigger("click");  //自動觸發事件//為所有的段落添加一個click事件,包括後來加上的元素    $("p").live("click",function () {        alert($(this).html());});


11.動畫效果 自訂animate

$("#go").click(function(){ $("#block").animate({height:300,width:'+800',marginLeft:'+=300'},"slow");}); //+800表示增加到800      +=800表示在原來基礎上加800// $(".panel").slideToggle("slow");  //上和下綜合 


12 .javascript原生綁定事件

var dg = document.getElementById("dg");dg.onmouseover =function(){     alert("good");     }

13.return false 在javascript中是跳出這個方法。  14. jquery 增加刪除class

$(thisObj).addClass("selected");$(thisObj).parents("ul").find("li").removeClass("selected");

15. 取select值

var pp = $("#provide option:selected").val();或var area0 = $("select[name='prov']").val();

16.在某元素中插入內容(可以是html,可以是字串)

在每個 p 元素結尾插入內容:

$("button").click(function(){ $("p").append(" <b>Helloworld!</b>");});


17. slideToggle 和 slideDown , slideUp

$(".clickdiv").click(function(){             $(this).slideToggle(500);  //通過使用滑動效果,在顯示和隱藏狀態之間切換 <p> 元素 slidedown 和slideup的結合           $(this).slideDown();  //顯示塊狀元素        });  




2 javascript 篇
1.尋找html元素  通過id尋找

var x=document.getElementById("intro");

本例尋找 id="main" 的元素,然後尋找 "main" 中的所有 <p> 元素:

var x=document.getElementById("main");var y= x.getElementsByTagName("p");
var  xx = document. getElementsByName('name');

  2. 本例改變了 某 元素的  屬性

<img id="image" src="smiley.gif">document.getElementById("image").src="landscape.jpg";


3.javascript事件 向 button 元素分配 onclick 事件:

<button id=” myBtn”>按我</button><script>document.getElementById("myBtn").onclick=function(){displayDate()};</script>

  4 .改變css

<p id="p2">Hello World!</p> <script>document.getElementById("p2").style.color="blue";</script>

5 .改變裡面的內容
<p id="p2">Hello World!</p> <script> document.getElementById("p2").innerHTML =”12333333333333";</script>

6 .連接字串
function ff(){ alert("abc"+"dfe");}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.