jQuery-操作html&遍历&ajax&noConflict&JSONP
操作html
获取内容和属性
获取内容
//显示文本
$("#btn1").click(function(){
  alert("Text: " + $("#test").text());
});
//显示html(带标签)
$("#btn2").click(function(){
  alert("HTML: " + $("#test").html());
});
//显示输入的值
$("#btn1").click(function(){
  alert("值为: " + $("#test").val());
});
获取属性
<script>
$(document).ready(function(){
  $("button").click(function(){
    alert($("#runoob").attr("href"));
  });
});
</script>
</head>
<body>
<p><a href="http://www.runoob.com" id="runoob">菜鸟教程</a></p>
<button>显示 href 属性的值</button>
</body>
设置内容和属性
同样使用text,html,val方法
$("#btn1").click(function(){
    $("#test1").text("Hello world!");
});
$("#btn2").click(function(){
    $("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
    $("#test3").val("RUNOOB");
});
设置属性
$("button").click(function(){
    $("#runoob").attr({
        "href" : "http://www.runoob.com/jquery",
        "title" : "jQuery 教程"
    });
});
设置内容和属性的callback都有回调函数,第一个参数:被选择元素当前下标;第二个参数:旧的值
添加元素
//结尾插入内容
$("p").append("追加文本");
//开头插入内容
$("p").prepend("在开头追加文本");
//可以通过追加,创建html文本
function appendText()
{
    var txt1="<p>文本。</p>";              // 使用 HTML 标签创建文本
    var txt2=$("<p></p>").text("文本。");  // 使用 jQuery 创建文本
    var txt3=document.createElement("p");
    txt3.innerHTML="文本。";               // 使用 DOM 创建文本 text with DOM
    $("body").append(txt1,txt2,txt3);        // 追加新元素
}
//之前之后添加
$("img").after("在后面添加文本");
$("img").before("在前面添加文本");
//添加新元素
function afterText()
{
    var txt1="<b>I </b>";                    // 使用 HTML 创建元素
    var txt2=$("<i></i>").text("love ");     // 使用 jQuery 创建元素
    var txt3=document.createElement("big");  // 使用 DOM 创建元素
    txt3.innerHTML="jQuery!";
    $("img").after(txt1,txt2,txt3);          // 在图片后添加文本
}
删除元素
//删除被选元素及子元素
$("#div1").remove();
//删除子元素
$("#div1").empty();
CSS类
先实现一个样式表
.important
{
        font-weight:bold;
        font-size:xx-large;
}
 
.blue
{
        color:blue;
}
例子
//向多个元素添加
$("button").click(function(){
  $("h1,h2,p").addClass("blue");
  $("div").addClass("important");
});
//添加多个css
$("button").click(function(){
  $("#div1").addClass("important blue");
});
//removeClass
$("button").click(function(){
  $("h1,h2,p").removeClass("blue");
});
//toggleClass
$("button").click(function(){
  $("h1,h2,p").toggleClass("blue");
});
css()方法
//返回 CSS 属性
//css("propertyname");
$("p").css("background-color");
//设置css属性
//css("propertyname","value");
$("p").css("background-color","yellow");
//设置多个 CSS 属性
//css({"propertyname":"value","propertyname":"value",...});
$("p").css({"background-color":"yellow","font-size":"200%"});
尺寸

//width() 和 height()
$("button").click(function(){
  var txt="";
  txt+="div 的宽度是: " + $("#div1").width() + "</br>";
  txt+="div 的高度是: " + $("#div1").height();
  $("#div1").html(txt);
});
//innerWidth() 和 innerHeight() 方法
$("button").click(function(){
  var txt="";
  txt+="div 宽度,包含内边距: " + $("#div1").innerWidth() + "</br>";
    txt+="div 高度,包含内边距: " + $("#div1").innerHeight();
  $("#div1").html(txt);
});
//outerWidth() 和 outerHeight() 方法
$("button").click(function(){
  var txt="";
  txt+="div 宽度,包含内边距和边框: " + $("#div1").outerWidth() + "</br>";
  txt+="div 高度,包含内边距和边框: " + $("#div1").outerHeight();
  $("#div1").html(txt);
});
遍历

祖先
//parent() 返回直接父元素
$(document).ready(function(){
  $("span").parent();
});
//返回所有祖先,它一路向上直到文档的根元素
$(document).ready(function(){
  $("span").parents();
});
//也可以指定参数过滤,<span> 元素的所有祖先,并且它是 <ul> 元素
$(document).ready(function(){
  $("span").parents("ul");
});
//parentsUntil()返回介于两个给定元素之间的所有祖先
$(document).ready(function(){
  $("span").parentsUntil("div");
});
后代
//children() 返回每个 <div> 元素的所有直接子元素
$(document).ready(function(){
  $("div").children();
});
//find() 可以加过滤 一路向下直到最后一个后代
$(document).ready(function(){
  $("div").find("span");
});
$(document).ready(function(){
  $("div").find("*");
});
同胞
siblings()  //所有同胞元素,可过滤
next()      //下一个同胞元素
nextAll()
nextUntil()
prev()
prevAll()
prevUntil() 
过滤
first(), last(), eq(), filter(), not()
选取首个 
 元素内部的第一个 
元素
$(document).ready(function(){
  $("div p").first();
});
$(document).ready(function(){
  $("div p").last();
});
//索引号从0开始
$(document).ready(function(){
  $("p").eq(1);
});
$(document).ready(function(){
  $("p").filter(".url");
});
$(document).ready(function(){
  $("p").not(".url");
});
ajax
load()
load() 方法从服务器加载数据,并把返回的数据放入被选元素中
$(selector).load(URL,data,callback);
//必需的 URL 参数规定您希望加载的 URL。
//可选的 data 参数规定与请求一同发送的查询字符串键/值对集合。
//可选的 callback 参数是 load() 方法完成后所执行的函数名称。
//把文件 "demo_test.txt" 的内容加载到指定的 <div> 元素中
$("#div1").load("demo_test.txt");
//把 "demo_test.txt" 文件中 id="p1" 的元素的内容,加载到指定的 <div> 元素中
$("#div1").load("demo_test.txt #p1");
回掉函数可以使用不同的参数
responseTxt - 包含调用成功时的结果内容
statusTXT - 包含调用的状态
xhr - 包含 XMLHttpRequest 对象
$("button").click(function(){
  $("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){
    if(statusTxt=="success")
      alert("外部内容加载成功!");
    if(statusTxt=="error")
      alert("Error: "+xhr.status+": "+xhr.statusText);
  });
});
get()和post()方法
//$.get(URL,callback);
//第一个回调参数存有被请求页面的内容,第二个回调参数存有请求的状态
$("button").click(function(){
  $.get("demo_test.php",function(data,status){
    alert("数据: " + data + "\n状态: " + status);
  });
});
//$.post(URL,data,callback);
//必需的 URL 参数规定您希望请求的 URL。
//可选的 data 参数规定连同请求发送的数据。
//可选的 callback 参数是请求成功后所执行的函数名。
$("button").click(function(){
    $.post("/try/ajax/demo_test_post.php",
    {
        name:"菜鸟教程",
        url:"http://www.runoob.com"
    },
        function(data,status){
        alert("数据: \n" + data + "\n状态: " + status);
    });
});
其他方法
noConflict
一些框架也使用$符号
MooTools、Backbone、Sammy、Cappuccino、Knockout、JavaScript MVC、Google Web Toolkit、Google Closure、Ember、Batman 以及 Ext JS
在jQuery中使用noConflict方法,释放$符号
$.noConflict();
jQuery(document).ready(function(){
  jQuery("button").click(function(){
    jQuery("p").text("jQuery 仍然在工作!");
  });
});
把noConflict()变成变量
var jq = $.noConflict();
jq(document).ready(function(){
  jq("button").click(function(){
    jq("p").text("jQuery 仍然在工作!");
  });
});
如果你的 jQuery 代码块使用 $ 简写,并且您不愿意改变这个快捷方式,那么您可以把 $ 符号作为变量传递给 ready 方法。
这样就可以在函数内使用 $ 符号了 - 而在函数外,依旧不得不使用 "jQuery":
$.noConflict();
jQuery(document).ready(function($){
  $("button").click(function(){
    $("p").text("jQuery 仍然在工作!");
  });
});
JSONP
JSON with Padding是一种json的使用模式,可以让网页从别的域名获取资料
http://www.runoob.com/json/json-jsonp.html
正常的jsonp代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JSONP 实例</title>
</head>
<body>
    <div id="divCustomers"></div>
    <script type="text/javascript">
function callbackFunction(result, methodName)
        {
            var html = '<ul>';
            for(var i = 0; i < result.length; i++)
            {
                html += '<li>' + result[i] + '</li>';
            }
            html += '</ul>';
            document.getElementById('divCustomers').innerHTML = html;
        }
</script>
<script type="text/javascript" src="http://www.runoob.com/try/ajax/jsonp.php?jsoncallback=callbackFunction"></script>
</body>
</html>
jQuery使用
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>JSONP 实例</title>
    <script src="http://cdn.static.runoob.com/libs/jquery/1.8.3/jquery.js"></script>    
</head>
<body>
<div id="divCustomers"></div>
<script>
$.getJSON("http://www.runoob.com/try/ajax/jsonp.php?jsoncallback=?", function(data) {
    
    var html = '<ul>';
    for(var i = 0; i < data.length; i++)
    {
        html += '<li>' + data[i] + '</li>';
    }
    html += '</ul>';
    
    $('#divCustomers').html(html); 
});
</script>
</body>
</html>
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号