if(window.location.hostname.indexOf("example.com") !== -1 ){ $("#nav3").hide(); $("#platform-nav li:nth-child(3)").hide(); $("#platform-nav li:nth-child(4)").hide(); $(".footer .fcon .coll").hide(); $(".footer .fcon .col3").hide(); $(".footer .fcon .col4").hide(); }
<a id="startMenu" href="#" class="more"> <section id="menu" class="right_menu disnone"> <dl> <dt><i></i></dt> <Dd>您好!創富金融歡迎您</Dd> </dl> <ul> <li><a href="#">首頁</a></li> <li><a href="#">簡介</a></li> <li><a href="#">...</a></li> <div style="clear:both;"></div> </ul> </section> <script> $(document).ready(function() { $("#startMenu").click(function() { if($("#menu").is(":visible")){ $("#menu").hide(); }else{ $("#menu").show(); } }); }); </script>
$("button").click(function(){ $("p:first").addClass("intro"); }); $( "p" ).removeClass( "myClass yourClass" ) $( "p" ).removeClass( "myClass noClass" ).addClass( "yourClass" ); <p>hello</p> <p id="hello">hello</p> <script type="text/javascript"> $("p").addClass("Helloworld"); $("#hello").addClass("Helloworld"); </script>
$(document).ready(function(){ setTimeout(function(){ $("#error").hide(); },3000); });
<p>hello</p> <p id="hello">hello</p> <script type="text/javascript"> $("p").text("Helloworld"); $("#hello").text("Helloworld"); </script>
返回值是數組的key
var host = window.location.hostname; var domains = ["netkiller.github.io","www.netkiller.cn"]; if(jQuery.inArray( host, domains ) != -1) { ... ... }
jQuery.ajax({ type:"GET", url: "/path/to/url", data: "code="+code, success:function(data){ if(data.status){ alert(data.text) } }, error: function(){ } });
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script> $.getJSON('http://www.foobar.com/json.php?callback=?', function(data){ alert(data.foo); }); </script> <?php echo $_GET['callback'], '(', json_encode(array('foo' => 'bar')), ')'; ?>
// Using YQL and JSONP $.ajax({ url: "http://query.yahooapis.com/v1/public/yql", // The name of the callback parameter, as specified by the YQL service jsonp: "callback", // Tell jQuery we're expecting JSONP dataType: "jsonp", // Tell YQL what we want and that we want JSON data: { q: "select title,abstract,url from search.news where query=\"cat\"", format: "json" }, // Work with the response success: function( response ) { console.log( response ); // server response } });
原因是 ajax 跨域請求造成
將 dataType: 'JSON' 替換為 dataType: 'JSONP'
Jquery ajax 請求預設是非同步方式,通過 async: false, 參數修改為同步模式。
function exchange(money){ var amount = 0; jQuery.ajax({ type: "GET", url: "ajax.php?money=" + money, dataType: "json", async: false, data: "", success: function (data) { if (data.amount) { amount = data.amount } }, error: function () { } }); return amount; }
var select = $('#bank'); //$('option', select).remove(); $.each(banklist, function(key,code) { var option = new Option(key, code) select.append( option ); }); $('#bank').append($("<option></option>").attr("value","CMB").text("招商銀行"));
$(document).ready(function() { $("#Button1").click(function() { $("#Tab1").hide(); $("#Tab2").show(); }); $("#Button2").click(function() { $("#Tab1").show(); $("#Tab2").hide(); }); });
解除事件綁定
$( "#Button1").unbind( "click" ); $( "#Button2").unbind( "click" );
事件中綁定事件
$("#Button").click(function() { $( "#Button1").unbind( "click" ); $( "#Button2").unbind( "click" ); $("#Button1").click(function() { $("#Tab1").hide(); $("#Tab2").show(); }); $("#Button2").click(function() { $("#Tab1").show(); $("#Tab2").hide(); }); });