首页 » JavaScript » 正文

最近项目中用到的js知识点

2016-9-5 ·  6,385 views  ·  1 replies 

1. 获取选中的select option的value值和文本值
[code lang=”js”] $("select[name=’demo’]").find(‘option:selected’).val();
$("select[name=’demo’]").find(‘option:selected’).text();
[/code] 2. 用js获取地址栏中的get参数
[code lang=”js”] /*—————–实现1——————–*/
function getPar(par){
//获取当前URL
var local_url = document.location.href;
//获取要取得的get参数位置
var get = local_url.indexOf(par +"=");
if(get == -1){
return false;
}
//截取字符串
var get_par = local_url.slice(par.length + get + 1);
//判断截取后的字符串是否还有其他get参数
var nextPar = get_par.indexOf("&");
if(nextPar != -1){
get_par = get_par.slice(0, nextPar);
}
return get_par;
}

/*——————–实现2(返回 $_GET 对象, 仿PHP模式)———————-*/
var $_GET = (function(){
var url = window.document.location.href.toString();
var u = url.split("?");
if(typeof(u[1]) == "string"){
u = u[1].split("&");
var get = {};
for(var i in u){
var j = u[i].split("=");
get[j[0]] = j[1];
}
return get;
} else {
return {};
}
})();

/*第2种方式, 使用时, 可以直接 $_GET[‘get参数’], 就直接获得GET参数的值*/
[/code] 3. protype和jquery的兼容问题
引入jquery,引入protype,然后在
[code lang=”js”] <script>jQuery.noConflict();
var $j = jQuery; </script>
[/code] 之后,就用$j代表$, 比如$(‘#id’)就改成$j(‘#id’)

«上一篇: :下一篇»
  1. 么么哒发表于:2016-09-20 08:18

    好,太棒啦

Leave a reply