js 记录遇到的坑

这里随手记录一下在js中遇到的坑吧:

  

Replace方法,默认只替换第一个

大坑货repalce:(默认只替换第一个)
string str ="wo shi js,ni shi shui"

1、替换第一个: string end =str.replace("shi","wei")
     结果:wo wei js,ni shi shui
2、想要全部替换的话: string end =str.replace(/shi/g,"wei") 结果:wo wei js,ni wei shui

  

 string和object对象的转换:

 1 <script>
 2     var jsonstr = ‘[{"date":1,"综合能率":"88","标准":"85"},{"date":2,"综合能率":"88","标准":"85"},{"date":12,"综合能率":"28","标准":"85"}]‘;
 3   //转json object对象
 4     var jsonObj = JSON.parse(jsonstr)
 5   //json对象再转回string
 6   var jsonstr2 = JSON.stringify(yyy);
 7     console.log(typeof(jsonstr));//string
 8     console.log(typeof(yyy));//object
 9     console.log(typeof(result));//string
10 </script>

 

$(‘#selectid‘).change(function () {
             var date = $(this).find("option:selected").val();
        //or 或者
var date1 = $(‘#selectid
option:selected‘).val();
        alert(date)
}