js对象转换成jquery对象:
jQuery(document).ready(function(){ var links = jQuery("#simple-menu li a"); for(var i=0;i<links.length;i++){ jQuery(links[i]).click(function(){ jQuery(".current").removeClass("current"); jQuery(this).addClass("current");//将js对象包装成jQuery对象 //这里this换成links[i],不会增加current类 }); } });
动态创建form:
<a href="/topics/1" onclick="if (confirm('你确定要删除这篇博客?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', '4k3y+j4gi85qFk7QQugsdEuWiCoOFrLPPLJgye7ERXg='); f.appendChild(s);f.submit(); };return false;">删除</a>
Handling runtime errors in JavaScript using try/catch/finally
alert("I am missing a closing parenthesis //syntax error alert(x) //exception assuming "x" isn't defined yet undefinedfunction() //exception
try{ undefinedfunction() } catch(e){ //catch and just suppress error }
try{ undefinedfunction() alert('I guess you do exist') } catch(e){ alert('An error has occurred: '+e.message) }
try{ undefinedfunction() alert('I guess you do exist') } catch(e){ alert('An error has occurred: '+e.message) } finally{ alert('I am alerted regardless of the outcome above') }
var ajaxrequest=null if (window.ActiveXObject){ //Test for support for different versions of ActiveXObject in IE try { ajaxrequest=new ActiveXObject("Msxml2.XMLHTTP") } catch (e){ try{ ajaxrequest=new ActiveXObject("Microsoft.XMLHTTP") } //end inner try catch (e){ alert("I give up. Your IE doesn't support Ajax!") } //end inner catch } //end outer catch } else if (window.XMLHttpRequest) // if Mozilla, Safari etc ajaxrequest=new XMLHttpRequest() ajaxrequest.open('GET', 'process.php', true) //do something with request
function change_list(){ var q=document.getElementById("search_input"); var str=trim(q.value); if(str!=""){ new Ajax.Request("/friend/find", { method: "post", parameters: "query="+str, evalScripts: true, onComplete: function(request) { element.fire("ajax:complete", request); }, onSuccess: function(request) { element.fire("ajax:success", request); }, onFailure: function(request) { element.fire("ajax:failure", request); } }); } }
function change_group(id,grp){ jQuery.ajax({ type:"post", url:"/friend/change_group", dataType: "json", data:{user_id: id,group: grp}, success:function (result, textStatus) { if(textStatus == "success"){ jQuery("#list"+result.jsonReturn[0]).hide(); jQuery("#group"+result.jsonReturn[0]).html(result.jsonReturn[1]); } } }); }